SwadhyAI Notes

All Java notes

Java Modifiers

›Short versionOpen if you only have a minute.
  • public is visible everywhere. private stays inside the top-level class. No keyword means package-private.
  • protected is visible in the same package and to subclasses. A subclass does not get unrestricted access to every superclass instance.
  • static attaches a member to the class. It does not widen or narrow access.
  • final on a class blocks subclassing. final on a method blocks overriding and makes the call static dispatch. final on a field blocks a second assignment.
  • abstract marks a class that cannot be constructed, or a method whose body a subclass must write.
  • Choose the narrowest access that lets the intended callers compile. Public fields become part of the API the day someone reads them.

A ledger has a counter page and a back office. Customers may ask for the balance. They may not reach over and rewrite the line. An is that counter: it decides which other classes are even allowed to mention the member.

Who can see it

Start from private for fields. Add a method when another class has a real question to ask. Leave the method when only the types that ship in the same package should call it. Use public when the member is part of the promise you are willing to keep.

java
public class Account {
    private int balance;

    public int balance() {
        return balance;
    }

    void auditNote(String note) {
        // visible in this package, hidden outside it
    }
}

sits between package-private and public. Same-package classes can use it, and subclasses elsewhere can use it to extend behavior. It is a poor choice for a field you hoped was "almost private." A subclass can still couple itself to that field, and so can every other class in the package.

What kind of member it is

and access answer different questions. You can have a public static constant, a private static helper, or a public instance method. The order you will see most often is access, then static or final or abstract, then the type and the name.

java
public static final int MAX_NAME = 40;
private static int nextId = 1;
public final void freeze() { }
public abstract class Shape { }

is three promises that share a keyword. A final class such as String has no subclasses. A final method cannot be overridden, so the compiler can bind the call to that body. A final field is assigned by the end of construction and then holds. Those are separate decisions. A class can have final fields and still be open for subclassing.

means the type is incomplete. new is rejected on an abstract class. A concrete subclass implements every abstract method it inherited and did not leave abstract. A method cannot be bothabstract and final, and it cannot be both abstract and private: nobody would be allowed to write the body.

A small account, marked on purpose

Account.java
public final class Account {
    private final String id;
    private int balance;

    public Account(String id, int opening) {
        this.id = id;
        this.balance = opening;
    }

    public int balance() {
        return balance;
    }

    public final void deposit(int amount) {
        balance += amount;
    }
}

The class is final, so nobody subclasses it and quietly changes deposit. The id is a final field.deposit is a final method, so even if the class were opened later, that method would stay statically dispatched. balance the field stays private. The public surface is the constructor, the query, and the deposit.

Terminal
$ javac Account.java
$ java Account

A helper method should be callable by classes in the same package, and not by the rest of the program. Which modifier do you write?

Write none. That is package-private. protected would also open the method to subclasses in other packages. private would hide it from the rest of the package too. Use private when even those neighbors should go through a public or package-private method.