permaculture playing cards*
The moose likes Beginning Java and the fly likes Packages And Import Statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Packages And Import Statement" Watch "Packages And Import Statement" New topic
Author

Packages And Import Statement

Craig Oliver
Greenhorn

Joined: Aug 25, 2003
Posts: 21
Is it true that you do not have to use import statement to import classes from same package. I am able to create own package containing related classes. These reference each other without using import statement. All seems okay, compiles and runs okay. Just wish to confirm this is regular convention or should I explicitly import.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
There is never a need to import classes that reside in the same package either for compiling or running unless of course something changes between compile time and runtime, like removing a class from the package directory or compiling one class and not recompiling an application. The import statement is only meaningful for compiling anyway, it has no effect at all at runtime. The runtime equivalent of import would be the CLASSPATH environment variable.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, Craig. What version of Java are you on? I think JDK 1.4 and later considers importing from the same package an error. At least the JavaDoc tool does! I had a project done in 1.2 something that I used 1.4 JavaDoc and I had to remove a bunch of import statements for the same package.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I think JDK 1.4 and later considers importing from the same package an error.
Not at all. This compiles just fine.The only changes to imports, that occurs to me, is that of importing items in the "default" package - the mysterious package classes belong to when they aren't defined to be in any package. Pre-Java 1.4 (or was it pre-1.3?), it was allowed to import a class in the default package, by simply using the statement "import MyClass;". In newer editions of Java (1.4+ and maybe 1.3+), this is not allowed.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Packages And Import Statement
 
Similar Threads
import packages?
classes in package cannot see other classes in same package
Why can't my class see another class?
Q of Khalid's inner class example
cannot find symbol error during compilation