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.

ModifierSame classSame packageSubclassEverywhere
privateyesnonono
(package / default)yesyesno*no
protectedyesyesyesno
publicyesyesyesyes

*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

  • private limits a member to its own class.
  • public opens a member to every class — use it for the API, rarely for fields.
  • protected and 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);
  }
}

FAQ: Java Access Specifiers

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java access specifiers examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java access specifiers in this Java Java lesson (Java Access Specifiers).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.