← Back to Week 5

Instance vs Static Members

An instance field lives inside each object — one copy per object. A static field lives on the class itself — one shared copy for everyone.

Static layer (shared by all instances) change once → everyone sees it
static double interestRate = edit this — watch every account update
Instance layer (one per new BankAccount(...)) each gets its own copy
public class BankAccount {
    private static double interestRate;     // SHARED
    private String number;                 // per instance
    private String name;                   // per instance
    private double balance;                // per instance

    public static double getInterestRate() {
        return interestRate;
    }
    public static void setInterestRate(double r) {
        BankAccount.interestRate = r;
    }
    public void deposit(double amount) {
        this.balance += amount;
    }
}
The mental rule: if every BankAccount needs its own value, it's an instance field. If every account should share the same value — a fee schedule, a tax rate, a global counter — it's static.

Static is also accessed differently: BankAccount.setInterestRate(0.045), not through an instance. You don't need to new anything to read or change a static.
← JUnit Assertions Picker Static Classes →