• 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

default (package) vs public

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is teh different between default package and public?
thanks in advance
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The default package has no name. All classes and interfaces that are not preceded by a package sentence, and that are in the same directory are placed in the same default package. That is, every directory has two packages the default an the package ends up with the same name as its directory.
However you should not use the default package except for testing purposes.
The classes that are in a named package must be placed in a directory path that reflects the package names. The parent directory of this directory structure must be placed in the classpath , otherwise the compiler/JVM won't find the classes in that package.
Now, are you asking about "public package"?
packages cannot have access modifiers, so in that sense all of them are public. The top level classes and interfaces in a package can be friendly or publicly accessed.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your question might be what's the difference between public accessibility and default (a.k.a) package accessibility???

here's a quick list from A Programmer's Guide to Java Certification:
public - Accessible everywhere
protected - Accessible by any class in the same packae as its class, and accessible only by subclasses of its class in other packages
default(no modifier) - Only accessible by classes, including subclasses, in the same package as its class (package accessibility).
private - Only accessible in its own class and not anywhere else.

 
Eric Gonzalez
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are right Jessica tha is what I meant, but the above reply was helpful too.
Is this correct?
least restrictive --------------->most restrictive
public,protected,default,private
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Eric
it's correct. default is between protected and private.
BTW, sometimes default access modifier is called as 'friendly'
Thanx,
Jamal Hasanov
www.j-think.com
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I always remember the default (modifier) by stating that it provides package accessibility (see Mughal's Java Cert. book).
default (no modifier): Only accessible by classes, including subclasses, in the same package as its class (package accessibility).
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic