Stop 04 · Classes & ObjectsTopics · 3/8

Access Modifiers

Part of the Java Roadmap

In short

Not every part of your code should be touchable from everywhere. Access modifiers let you decide what is public, what stays in the family, and what is strictly private, which is how large Java codebases stay safe to change.

public, protected, package-private (no modifier) and private control where classes, fields and methods are visible. Fields are usually private to protect an object's state.

The jaadu stands in a cutaway house with an open front verandah, a cosy sitting room and a small room with a padlocked cupboard.

Think of it like this

Rooms in a family home

The front verandah is open to anyone passing by: public. The drawing room is for relatives and close friends: protected. The kitchen is for people living in the house: package-private. Your personal cupboard with a lock is for you alone: private.

Watch it happen

Who can see a member, from most open to most closed

public
0
protected
1
(no modifier)
2
private
3

1/5Four levels. Pick the most restrictive one that still works.

The big ideas

What you need to know

Fields private

Keep data private and expose behaviour through methods. Then you can change the inside without breaking callers.

public is a promise

Anything public becomes part of your API. Others will depend on it, so make it public only on purpose.

Package-private by default

Forgetting a modifier gives package access, which is wider than private. It is not the same as private.

Top-level classes

A top-level class can be public or package-private only. One public class per file, matching the file name.

See it in code

Walk through the code

java
1package com.shop.orders;2 3public class Order {4    private final String id;5    private double amount;6    protected String status = "NEW";7    String createdBy;                 // package-private8 9    public Order(String id, double amount) {10        this.id = id;11        this.amount = amount;12    }13 14    public double getAmount() {15        return amount;16    }17 18    public void applyCoupon(double discount) {19        if (isValidDiscount(discount)) {20            amount -= discount;21        }22    }23 24    private boolean isValidDiscount(double d) {25        return d > 0 && d <= amount * 0.5;26    }27}

1/5private: no other class can read or change id and amount directly.

Private data, a private helper, one protected field for subclasses, and a small public API.

Save yourself the pain

Common mistakes

Oops

Making every field public to save time.

Do this instead

Any code can then set amount = -500. Keep fields private and validate in methods.

Oops

Assuming no modifier means private.

Do this instead

It means package-private. Write private explicitly when that is what you want.

Your mission

Lock it down

~15 min

Quick check

A field has no access modifier. Who can access it?

Go deeper

  1. 1Make a table of what each modifier allows within a class, package and subclass.
  2. 2Make all fields in your classes private.
  3. 3Try accessing a private field from another class to see the compile error.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge