File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes default (package) vs public Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "default (package) vs public" Watch "default (package) vs public" New topic

default (package) vs public

Eric Gonzalez

Joined: May 01, 2002
Posts: 10
What is teh different between default package and public?
thanks in advance
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.

SCJP2. Please Indent your code using UBB Code
Jessica Sant

Joined: Oct 17, 2001
Posts: 4313

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.

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Eric Gonzalez

Joined: May 01, 2002
Posts: 10
you are right Jessica tha is what I meant, but the above reply was helpful too.
Is this correct?
least restrictive --------------->most restrictive
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Yes, Eric
it's correct. default is between protected and private.
BTW, sometimes default access modifier is called as 'friendly'
Jamal Hasanov
Ricardo Cortes
Ranch Hand

Joined: Jan 23, 2002
Posts: 140
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).

Sun Certified J2EE Architect for the J2EE Platform (Part 1)<br />Sun Certified Web Component Developer for the J2EE Platform<br />Sun Certified Programmer for the Java 2 Platform
I agree. Here's the link:
subject: default (package) vs public
It's not a secret anymore!