Re: importing external class from inside package
prakhargoyal@gmail.com wrote:
The structure of my java program is as follows:
Inside directory 'winter', I have a 2 java files A.java and B.java
which both belong to the package 'winter'.
Inside directory 'winter', I have 2 other files otherA.java and
otherB.java which do not belong to any package.
Directory structure and package structure must match.
Is this what you're describing?
rootdirectory/
|__ winter/
|_____ A - in package "winter"
|_____ B - in package "winter"
|_____ otherA - not in any package
|_____ otherB - not in any package
?
If so, then "otherA" and "otherB" are in the wrong directory; they need to be
in "rootdirectory/". The package "winter" is one "level down" from the root,
as is its subdirectory "winter/". The default package is at the root, and it
is at the root directory that you have classes that have no package.
Also, you should get in the habit of naming all your classes with an initial
upper-case letter: "OtherA", not "otherA".
Now I want A to access a class from otherA.java and otherA,java to
access a class from A.java.
I have achieved the latter using the following import statement:
import winter.A;
However, I cannot achieve the other case. In short, how do you access
an outer class which is not part of any package from inside a package?
You cannot import classes from the default package.
You should not use the default package anyway. You absolutely should not use
it for any code you deploy publicly.
- Lew