Access Modifiers in Java

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.

Access ModifierDescription
publicAccessible to anyone who cares to access. E,g, All elements of an API – classes and methods should be public.
protectedAccessible from within the same package and by subclasses
privateAccessible from within the same class. Can not be applied to class
defaultWhen 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

  1. class
  2. instance methods
  3. static methods
  4. instance variables
  5. 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 classImplication of declaration
publicThe 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

  1. public
  2. private
  3. protected
  4. 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

  1. From within the same class
  2. From within the same package
  3. From a sub class
  4. 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-

  1. public variable or method is visible from everywhere
  2. private variable or method is visible from nowhere except for the class where it is defined
  3. protected variable or method is visible only in the package where it is defined and in the subclasses
  4. 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 –

 publicprotecteddefaultprivate
Visible from same Class?
Visible from same Package?
Visible from a Subclass?
Visible from the rest of insane world?

Guidelines on Choosing Access Modifers

  1. Make the visibility such that only entities with a need-to-know are able to see your classes, methods and variables.
  2. 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.
  3. Make all your instance variables private. By virtue of Java’s design and coding methodology, this requirement is non-negotiable.
  4. static variables may be made public. public static variables should also be final. A non final public static variable reflects poorly on your design.
  5. Prefer public classes. Non-public classes and inner classes, if used non-judiciously, quickly turn into a maintenance nightmare.

Leave a comment

Your email address will not be published.