Tools
Log in
English

Learn to codeJava

Java access modifiers

Based on Java 17

Java public, protected, package-private and private in one table.

In one line

Words placed before classes, fields and methods to set how far they are visible. From widest to narrowest: public, protected, no modifier, private.

Why you need it

If the safe’s code field is public, any code can read it as home.code. If the failure count is open, one line, fails = 0, unlocks a locked safe (mistake 2 below).

A narrow scope means fewer places to search when you ask "who changed this value?". For a private field, the answer is always inside one class file.

Everyday picture

Picture an apartment complex. Anyone can read its name (public), only residents use the playground (no modifier, same package), the caretaker and whoever takes over the job hold the office key (protected), and only the owner opens the safe at home (private).

Example code

Main.java · 46 lines
class Safe {    public final String name;    protected int maxTries = 3;    String note = "";    private final String code;    private int fails;     Safe(String name, String code) {        this.name = name;        this.code = code;    }     public boolean open(String guess) {        if (fails >= maxTries) {            System.out.println(name + " is locked");            return false;        }        if (code.equals(guess)) {            fails = 0;            System.out.println(name + " opened");            return true;        }        fails++;        System.out.println("Wrong code (" + fails + "/" + maxTries + ")");        return false;    }} class HotelSafe extends Safe {    HotelSafe(String name, String code) {        super(name, code);        maxTries = 5;    }} public class Main {    public static void main(String[] args) {        Safe home = new Safe("Home", "2580");        HotelSafe room = new HotelSafe("Room 301", "1004");        home.note = "spare key inside";        home.open("1111");        home.open("2580");        room.open("0000");        System.out.println(home.name + " / note: " + home.note);    }}

Where each one is visible

Same classSame packageSubclass in another packageOther package
publicOOOO
protectedOOOX
(none)OOXX
privateOXXX

protected sounds like "children only", but any class in the same package can see it too. A top-level class can only be public or have no modifier.

When to use it

  • Start every field as private and open only the actions outside code needs as public methods.
  • Leave helpers used only by classes in the same package with no modifier.
  • Open the spots subclasses should replace as protected methods.

A file can hold only one public class, and the file name must match it.

Common mistakes

Reading a private field from outside

code is visible only inside Safe. From outside, ask whether a guess is right with open.

Wrong

public class Main {    public static void main(String[] args) {        Safe home = new Safe("Home", "2580");        System.out.println(home.code);    }}

What javac sayserror: code has private access in Safe

Fixed

public class Main {    public static void main(String[] args) {        Safe home = new Safe("Home", "2580");        System.out.println(home.open("2580"));    }}

When you run ittrue

Making everything public for convenience

Outside code unlocked the safe with a single fails = 0. Keep fields private and open only meaningful methods such as fail().

Wrong

class Safe {    public int fails;    public int maxTries = 3;     public boolean locked() {        return fails >= maxTries;    }} public class Main {    public static void main(String[] args) {        Safe s = new Safe();        s.fails = 3;        s.fails = 0;        System.out.println(s.locked());    }}

When you run itfalse

Fixed

class Safe {    private int fails;    private final int maxTries = 3;     public void fail() {        fails++;    }     public boolean locked() {        return fails >= maxTries;    }} public class Main {    public static void main(String[] args) {        Safe s = new Safe();        s.fail();        s.fail();        s.fail();        System.out.println(s.locked());    }}

When you run ittrue

Two public classes in one file

A file can hold only one public class, and it must match the file name. Remove public from Safe or move it to Safe.java.

Wrong

public class Safe {    private int fails;} public class Main {    public static void main(String[] args) {        System.out.println("hi");    }}

What javac sayserror: class Safe is public, should be declared in a file named Safe.java

Fixed

class Safe {    private int fails;} public class Main {    public static void main(String[] args) {        System.out.println("hi");    }}

When you run ithi

Check yourself

Question 1

Where is a field with no modifier visible?

Show the answer

Answer: 2. Only inside the same package With no modifier it is visible only inside the same package. Even a subclass in another package cannot see it.

Question 2

Where can a protected field be seen?

Show the answer

Answer: 2. The whole package plus subclasses in other packages protected is the package-level range plus subclasses in other packages.

Questions people ask

What does no modifier mean?

Visible only inside the same package (package-private). Classes in other packages cannot see it, not even subclasses.

Is protected visible only to subclasses?

Every class in the same package can see it, plus subclasses in other packages.

Can a class itself be private?

A top-level class can only be public or have no modifier. A class nested inside another class can be private.