• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Packages

 
Rupesh Bonthala
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic