wood burning stoves 2.0*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Packages" Watch "Packages" New topic
Author

Packages

Rupesh Bonthala
Greenhorn

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 Test.java 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é
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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
Sheriff

Joined: Oct 13, 2005
Posts: 37985
    
  22
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.
 
jQuery in Action, 2nd edition
 
subject: Packages
 
Similar Threads
Importing Alternate Layout Managers
loading a fellow class from a package
What is the disadvantage of using "*" in an import statement??
about import statement
packages question