File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Packages" Watch "Packages" New topic


Rupesh Bonthala

Joined: Mar 08, 2007
Posts: 5
is it compulsory for a class to be part of the package to import it?
for example i've a file as below
public class Test
void tell()
System.out.println("hi i'm tell from Test class");

is it possible to import this class. (This is not part of any package).
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

It's not compulsory, unnamed packages are supported. But you can only refer a class in an unnamed package from the same package. Using unnamed package should be avoided though, and as the Java Specification states, "Unnamed packages are provided by the Java platform principally for convenience when developing small or temporary applications or when just beginning development".

[My Blog]
All roads lead to JavaRanch
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46437
Welcome to JavaRanch

Please use the code button and maintain indentation; it makes the code much easier to read.

No. A class in the "default" or "unnamed" package is only accessible to other classes in the default package, and cannot be imported by classes in other packages. I am told it used to be possible, but if you look in the Java Language Specification and find �7.5 it says it is not permitted.
I agree. Here's the link:
subject: Packages
jQuery in Action, 3rd edition