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 Copy Edit it Reset code 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); } }
Java code does not run on this page. Compile your changed code with javac on your own computer.
Step through it Not started Press Next to move one line at a time from the first line of main. The bright bar is the line that just ran.
Start Back Next
Output Wrong code (1/3) Home opened Wrong code (1/5) Home / note: spare key inside We ran this with javac and java 17 beforehand.
Class diagram Safe + name: String # maxTries: int ~ note: String - code: String - fails: int + open(String): boolean HotelSafe ~ HotelSafe(String, String) + public · - private · # protected · ~ no modifier (same package) · underlined static · italic abstract
Where each one is visible Same class Same package Subclass in another package Other package publicO O O O protectedO O O X (none)O O X X privateO X X X
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 says error: 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 it true
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 it false
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 it true
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 says error: 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 it hi
Check yourself Question 1
Where is a field with no modifier visible?
1 Everywhere 2 Only inside the same package 3 Only inside the same class
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?
1 Only in subclasses 2 The whole package plus subclasses in other packages 3 Only in the same class
Show the answer Answer: 2. The whole package plus subclasses in other packages protected is the package-level range plus subclasses in other packages.
Read next Previous Composition and static Next Project structure Related Encapsulation 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.