Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Packages And Import Statement

 
Craig Oliver
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic