EMZETT.
Login

Modifiers

In short: Keywords in front of a class, method, or variable that determine its visibility and behaviour — e.g. public, private, static, final.

In more detail: Access modifiers regulate where something can be reached from: public (everywhere), protected (package + subclasses), no modifier/“package-private” (only within the same package), and private (only within the same class). static binds an element to the class instead of individual objects, final makes a variable immutable or a class/method no longer extendable/overridable.

In Depth

public class Account {
    private double balance;              // only visible within this class
    protected String accountType;          // class + package + subclasses
    String description;                // "package-private" (no modifier) - only within the same package
    public String owner;              // visible from everywhere
 
    private static int accountCount = 0; // belongs to the CLASS, shared across all instances
    public static final double MIN_BALANCE = 0.0; // constant: static + final combined
 
    public final void lockedMethod() { // final = CANNOT be overridden
        System.out.println("This implementation is fixed");
    }
}
 
public final class ImmutableClass { } // final at the class level = no subclasses allowed

The four visibility levels form a clear hierarchy from “least open” to “most open”: private (same class only) < package-private/no modifier (same package only) < protected (package + subclasses, even outside the package) < public (everywhere). Rule of thumb when designing your own classes: start as restrictively as possible (private) and only open up when actually needed — that’s encapsulation in practice. static final combined (like MIN_BALANCE above) is the common pattern for true constants: static, because the value applies independently of individual objects, final, because it never changes again after initialization.

See also: Encapsulation, Classes, Constants (final)