Shiklo Programming
EN
  • English
  • Español
  • Português
  • Deutsch
  • Français
  • 日本語
  • हिन्दी

Java Modifiers

Words like public, private, static and final in front of a class, field or method are called modifiers. They change what the item is or who may use it. You have already used several of them; this topic puts them together.

Access modifiers

Access modifiers decide who can use a class, field, method or constructor:

ModifierUsable from
publiceverywhere
protectedthe same package, and subclasses
(none)only the same package ("package access")
privateonly inside the same class

A package is a group of related classes; the Packages topic shows how to make them. Subclasses come in Inheritance. For now the most important pair is public and private.

private: keep it inside

A private field can only be used by code inside its own class. Other classes get a compile error:

class BankAccount {
    private double balance = 1000;
}

public class Main {
    public static void main(String[] args) {
        BankAccount acc = new BankAccount();
        acc.balance = -500;
        System.out.println(acc.balance);
    }
}
The compiler says
Main.java:8: error: balance has private access in BankAccount

The compiler stops the bad change before the program can even run. To let other classes work with a private field in a controlled way, the class offers public methods:

class BankAccount {
    private double balance = 1000;

    public double getBalance() {
        return balance;
    }

    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }
}

public class Main {
    public static void main(String[] args) {
        BankAccount acc = new BankAccount();
        acc.deposit(250);
        acc.deposit(-999);
        System.out.println(acc.getBalance());
    }
}
Output
1250.0

The negative deposit is ignored, because deposit checks its input. This idea is called Encapsulation, the subject of the next topic.

Non-access modifiers

ModifierOn a fieldOn a methodOn a class
staticone copy for the classcalled without an object(for nested classes)
finalvalue set only oncecannot be overriddencannot be extended
abstract–has no body; subclasses provide itcannot be created with new

You met static and final fields in Class Attributes. abstract is covered in Abstraction. Modifiers can be combined, for example public static final.

Why main is public static void

Now every word of the main method makes sense:

public class Main {
    public static void main(String[] args) {
        System.out.println("public: Java can call it from outside the class.");
        System.out.println("static: it runs without creating a Main object.");
        System.out.println("void: it returns nothing.");
        System.out.println("args: it receives command-line words, here " + args.length + ".");
    }
}
Output
public: Java can call it from outside the class.
static: it runs without creating a Main object.
void: it returns nothing.
args: it receives command-line words, here 0.

Choosing modifiers

A good default: make fields private, make the methods other classes need public, and keep helper methods private. Give the smallest access that works; it is always easy to open something up later and hard to close it once other code depends on it.

Practice exercises

  1. Make the fields of a Student class private and try to read one from Main. Read the error.
  2. Add a public method getName() and use it instead.
  3. Make a private helper method inside a class and call it from a public method of the same class.