• 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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic