Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Keywords and Statements

Java access modifiers: public, protected, private

Reference for Java access modifiers: what public, protected, private and package-private allow, where each applies, and common mistakes with them.

Access modifiers decide which code may use a class, field, method, or constructor. Java has four access levels: public, protected, private, and the default level you get by writing no modifier at all, called package-private. Choosing the narrowest level that works is the main tool of encapsulation: it lets you change internals later without breaking callers.

The modifier goes at the start of a declaration. A top-level class can only be public or package-private; members and nested types can use any of the four levels.

Java
public class Account {
    private long balanceCents;          // only this class (and its nested types)
    int version;                        // package-private: same package only
    protected String owner;             // same package + subclasses
    public long balance() { return balanceCents; }   // everyone
}

Try it

Java Private access inside one top-level class

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Protected members and subclasses

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Modifier Same class Same package Subclass in another package Everywhere
private yes no no no
(none, package-private) yes yes no no
protected yes yes yes (through inheritance) no
public yes yes yes yes
Declaration Allowed levels
Top-level class, interface, enum, record public or package-private
Nested (member) class all four
Field, method, constructor all four
Interface member (method, field) public (implicit); private for helper methods since Java 9
Enum constructor private (implicit) or package-private
Local variable, local class none (scope is the block)

Usage notes

  • private is per class, not per object. An instance can read the private fields of another instance of the same class, and nested classes share private access with their enclosing class (as shown above). Two unrelated top-level classes cannot.
  • protected has a twist across packages. In a subclass in another package you may use the member through this or through references of the subclass type, but not through a plain reference to the superclass type. Within the same package it works like package-private plus inheritance.
  • Overriding cannot reduce access. An overriding method must be at least as accessible as the one it overrides; you can widen protected to public, never narrow it. Interface methods are public, so implementations must be declared public (a frequent compile error).
  • Private members are not inherited, and private methods are not overridden (a subclass method with the same name is simply a new method).
  • Constructors: a private constructor stops outside code from calling new, which is the basis of singletons, static factories, and utility classes. A class with no declared constructor gets a default one with the same access as the class.
  • Records and enums: record components become private final fields with public accessors; enum constructors are always private.
  • Reflection and modules: setAccessible(true) can bypass access checks in classpath code, but the module system can forbid it (strong encapsulation of JDK internals). Never design around it.
  • Package-private is a real tool, not a missing keyword. It is good for helpers shared inside a library but hidden from users, and for testing code in the same package.
  • Default to private for fields. Expose behavior through methods, and make a class public only when it is part of your API.
  • Modules add one more layer: a public class in a package that is not exports-ed by its module is still invisible to other modules. See module.