This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes How To access a class that has same name as an inner class ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How To access a class that has same name as an inner class ?" Watch "How To access a class that has same name as an inner class ?" New topic
Author

How To access a class that has same name as an inner class ?

Ibrahim AlShehri
Greenhorn

Joined: Jan 29, 2007
Posts: 12
Hi all,


Is it possible to to access ABC class (not the inner one) from InClassTest

provided that InClassTest has an inner class called ABC





thanks.


SCJP 5.0 (87%)
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Interesting question !!!
Some basic fundamental:
1) "Whenever you refer to a class without a package name(not imported) then you refer to the package of the class you are working on."
2) The package of an inner class is the same as the containing class appended with the name of the outer class. In your code the package of inner ABC will be InClassTest(name of the containing class)(You can test this by opening the compiled class using jad). But, inside the containing class you have an option of not specifying this package name, the compiler does it for you.
3) One can not import any class from the default package from a named package.

Now, in your case, there is a class in the default package named ABC and there is a class in the named package InClassTest with the same name ABC.
But, since the class in the named package is an inner class so you have the option of using the class name without its package. This relaxation comes with a burden though .. now, you lose references to any class from default package of the same name. Also, you can not import that class since its in the default package

This is not a big issue , since as a best practise, one always defines classes under a named package. If all your classes are defined under a package(eg test), you can refer to the outer class as test.ABC and the inner one as just ABC or alternatively as test.InClassTest.ABC.


apigee, a better way to API!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How To access a class that has same name as an inner class ?
 
Similar Threads
Outer class is able to access inner class private members?
Access to "hidden" outer variables
Tricky Class Name..?
Nested & Inner class
Accessing outer class members from instance of inner class