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.
The moose likes Beginning Java and the fly likes packaged class using unpackaged class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "packaged class using unpackaged class" Watch "packaged class using unpackaged class" New topic
Author

packaged class using unpackaged class

David Duran
Ranch Hand

Joined: Feb 11, 2002
Posts: 122
Here's a quote from this site http://bdn.borland.com/article/0,1410,26969,00.html

If no package name is specified in a class or interface then that class or interface is said to be part of the "default" or "unnamed" package. Note that if you place your class or interface in the default package then all other classes or interfaces in all other packages will have access to that class (unless it is declared private). For this reason it is considered good practice to put all classes and interfaces in packages.

Unfortunately, that doesn't work me.
For example, I'll have those unnamed classes in a jar and put them in my classpath and reference them in another unnamed class. Works.
As soon as I put a package statement on MY unnamed class, it says it can't find the (unnamed) classes in the jar file.
Am I missing something? I assume from that quote I can reference the class as if it were part of the same package in the class, ie. no imports of the class are needed.
David Duran
Ranch Hand

Joined: Feb 11, 2002
Posts: 122
Apparently this page says the opposite of that quote - http://www.cs.wisc.edu/~hasti/cs368/JavaTutorial/NOTES/Packages.html
I guess unnamed-package classes can not use named-package classes.
Matt Cao
Ranch Hand

Joined: Apr 03, 2003
Posts: 715
Hi David,
Good detective practice. The other article using package in the POV of JBuilder not in term of the Java language.
Cheers,
MCao
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I guess unnamed-package classes can not use named-package classes.
Or the other way around perhaps - classes in named packages cannot use classes in the unnamed package, while classes in the unnamed package can use classes in named classes, provided the CLASSPATH is set correctly and the correct import statements are used.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: packaged class using unpackaged class