Java in Notes/Domino Explained: Visibility modifiers


The Java programming language has 4 access modifiers:

  • public
  • private
  • protected
  • (none) aka “friendly”

Below I’ll tell you a little about each of the modifiers.

Modifier Description
public This is the least restrictive of the modifiers and it should be understood as literal as the word. Member variables and methods are visible to code inside and outside the class independent on class inheritance hierarchy and package.
private This is the most restrictive of the modifiers. Member variables and methods are private to the inclosing class and isn’t visible to any code outside the class itself. Variables and methods marked private are however visible to inner classes.
protected Protected is somewhere in between public and private and means that the variable or method is visible to the inclosing class and its subclasses. This makes it very usable when designing class hierarchies since it allows you to put code in a generic super class without making it visible to outside classes.
(none) aka “friendly” Leaving out the modifier gives the variable or method the same properties as a public variable or method but only inside the same package. This is beneficial if you are designing an API and you need some public methods that shouldn’t be exposed to outside code. Be aware that there isn’t any mechanism for disallowing programmers to create new classes in your packages so caution should still be exhibited when using the “friendly” modifier.