Java Tutorial
Java Access Specifiers
public, private, and protected control who can read and write members. Start private for fields; open only what other classes truly need.
Who can see this member?
Access modifiers sit on classes, fields, and methods. They are how Java enforces boundaries between parts of a program — not optional style, but compiler-checked rules.
| Modifier | Same class | Same package | Subclass | Everywhere |
|---|---|---|---|---|
private | yes | no | no | no |
| (package / default) | yes | yes | no* | no |
protected | yes | yes | yes | no |
public | yes | yes | yes | yes |
*A subclass in another package does not get package-private access.
private keeps fields inside
Mark fields private so only methods of the same class can touch them. Outside code must go through those methods — which is the door to encapsulation in the next lesson.
Example
class Account {
private double balance;
Account(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
Account a = new Account(50);
System.out.println(a.getBalance());
// a.balance = -10; // compile error: balance has private access
}
}public is the open door
public members are usable from any class. Constructors and methods that form the class's API are often public. Fields usually should not be — once every caller writes them directly, you cannot add rules later.
Example
class Greeter {
public void hello(String name) {
System.out.println("Hello, " + name);
}
}
public class Main {
public static void main(String[] args) {
new Greeter().hello("Ada");
}
}protected matters most with inheritance: subclasses (even in other packages) can see protected members. Prefer private until a subclass genuinely needs access.
Try It Yourself
Exercise: Make score private on Player. Add a publicgetScore() and addPoints(int). From main, add points and print the score without touching the field directly.
Show solution
class Player {
private int score;
public void addPoints(int pts) {
score += pts;
}
public int getScore() {
return score;
}
}
public class Main {
public static void main(String[] args) {
Player p = new Player();
p.addPoints(10);
System.out.println(p.getScore());
}
}Outside code uses the public methods; the private field stays hidden inside the class.
Key Takeaways
privatelimits a member to its own class.publicopens a member to every class — use it for the API, rarely for fields.protectedand package-private sit between those extremes.- Default to private fields; widen access only when another class needs it.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Engineering
Public telemetry field
public means any class can read the field. Fine for a status string; dangerous for a calibration constant you meant to lock.
Example
class Device {
public String status = "ok";
}
public class Main {
public static void main(String[] args) {
System.out.println(new Device().status);
}
}