It's not a secret anymore!
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Packages And Import Statement" Watch "Packages And Import Statement" New topic

Packages And Import Statement

Craig Oliver

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

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:
subject: Packages And Import Statement
It's not a secret anymore!