• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ibrahim AlShehri
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic