The Source for Java Technology Collaboration

As the new Java.net infrastructure contains project-level wikis, this main wiki will be shut down in the near future. For wiki page export and general wiki questions please contact the site admin at communitymanager@java.net.
Home | Help | Changes | Index | Search | Go

AccessModifier

In Java, there are three keywords used to specify the access level of classes. These three access modifiers are: public, protected and private.

A variable, method or class (also known as a feature) can only be defined with at most one access modifier. Local variables defined within methods cannot have access modifiers. An access modifier is not required when defining a feature; default access will be applied to any feature defined without an access modifier.

public access

Any variable, method or class defined with the public modifier may be used within any other class without restriction.

protected access

Any variable or method defined with the protected modifier may be used by all classes within the same package. As well, protected variables and methods are accessible to any class that is a subclass of the class containing the protected variable or method, even if the subclass is contained within another package.

Note that classes cannot be declared with protected access.

private access

Any variable or method defined with the private modifier may be used only within the class that defines the variable or method.

default access

Any variable, method or class not explicitly defined with an access modifier is given the access level of default. In this case, such a feature may be used by all classes within the same package. A subclass defined within another package from its superclass, cannot access the variables or methods with default access of the superclass. People often call this level of access package private.

Default access is also known as "friendly" or "package" access. The use of "friendly" is discouraged since it may lead to confusion with a similar concept from C++.



Discussion about AccessModifier

I really miss a modifier for accessing a member from a subclass, but not from any other class (even in the same packege). I think in Java 1 there was a 'private protected'. I never understood why this is not available. -- [Unsigned entry]

In English, "public" and "private" are opposites. In Java, this is not the case, since there are 4 scoping levels, as stated above. In speaking casually about scope in Java, one often hears the terms public and private being used as if they were strict opposites. Here is an example - "Do not use assertions for argument checking in public methods." The intention seems to be "non-private", not "public". This is imprecise, however, and should probably be avoided. Although it is less natural to utter, one could use "non-private" instead of "public" in many such cases. -- JohnOHanley - 31 Oct 2003

Does anyone else find it disturbing to see that "public" often seems to be used where "package-private" should be used instead? For example: in the DAO pattern, the intent is to keep secrets about implementation of the datastore. Thus, the data package defines interfaces to be used by the rest of the program. Implementations of said interfaces should clearly be package-private, in order to enforce the fact that they should not be known to the rest of the program. Many examples, however, make such classes public! This is erroneous. Example - Core J2EE Patterns, 2001, page 404. "public class CustomerDAOCloudscape ". -- JohnOHanley - 31 Oct 2003

Topic AccessModifier . { Edit | Ref-By | Printable | Diffs r5 < r4 < r3 < r2 < r1 | More }
 XML java.net RSS

  

Revision r5 - 2003-11-01 - 01:21:55 - JohnOHanley