Access modifiers specify who can access what data. In a secure object-oriented programming language such as Java, there are stricts checks and balance about what chunk of code and data is accessible, by whom and what point in execution. Access modifiers form the building blocks of the object oriented paradigms of encapsulation and data hiding. Data hiding principle commands (to the lowly programmers) that information should be available strictly on a need-to-know basis. Access modifiers in Java enforce this requirement.
The 4 Access Modifiers in Java
The following table summarizes the four Java access modifiers. They will be expanded in the subsequent sections.
|public||Accessible to anyone who cares to access. E,g, All elements of an API - classes and methods should be public.|
|protected||Accessible from within the same package and by subclasses|
|private||Accessible from within the same class. Can not be applied to class|
|default||When an access modifier is not specified, Java has sensible defaults. The defaults vary depending on the item on which access is being determined.|
Which Elements Can Have Access Modifiers?
The above mentioned access modifiers can only be specified on the following constructs
- instance methods
- static methods
- instance variables
- static variables
Access Modifiers for class
A class construct can be qualified as either having public or default, a.k.a package-private. When you do not explicitly make a class public by saying so in its declaration, the class becomes package-private.
Java does not support private or protected classes. This might be shocking to C++ programmers, and rightly so.
The table below summarizes the points discussed above.
|Access modifier of class||Implication of declaration|
|public||The class is visible to all other classes. Needs to be defined in the file named as the class.|
|default (no access modifier specified)||Class is accessible only within other classes of the same package|
Access Modifiers for Variables and Methods
Just to reiterate the scope of this discussion, we have already seen that local variables cannot have access modifiers.
Variables (both static and instance) and methods (again, both instance and static) can be asssociated with the following access modifiers
- default (no acces modifier specified explicitly)
A variable or method depending on the access modifiers, may or may not be accessed from the following code locations
- From within the same class
- From within the same package
- From a sub class
- From outside package (the rest of the evil world)
Rules of the Game
If you remember the following four golden rules, you can always work out where your variables and methods would be visible-
- A public variable or method is visible from everywhere
- A private variable or method is visible from nowhere except for the class where it is defined
- A protected variable or method is visible only in the package where it is defined and in the subclasses
- A variable or method whose access modifier is not specified is visible only inside the same package
Summary of Access Modifiers and Visibility
The following table summarizes the visibility rules disccussed above –
|Visible from same Class?||☑||☑||☑||☑|
|Visible from same Package?||☑||☑||☑||☓|
|Visible from a Subclass?||☑||☑||☓||☓|
|Visible from the rest of insane world?||☑||☓||☓||☓|
Guidelines on Choosing Access Modifers
- Make the visibility such that only entities with a need-to-know are able to see your classes, methods and variables.
- Try to make your instance and static methods private. Expose only a top level method. This public method can in turn call several private methods, each doing a smaller task.
- Make all your instance variables private. By virtue of Java’s design and coding methodology, this requirement is non-negotiable.
- static variables may be made public. public static variables should also be final. A non final public static variable reflects poorly on your design.
- Prefer public classes. Non-public classes and inner classes, if used non-judiciously, quickly turn into a maintenance nightmare.
We are socialSpread the word