This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
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.
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
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
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.