• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Packages And Import Statement

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic