Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Classes and Objects  ›  Capstone Project

The static and final Keywords

Static Keyword 14 minA static member is shared by the whole class rather than copied into each object, and final means a variable is assigned once, a method cannot be overridden and a class cannot be extended.

The static and final Keywords

The manager wants to know how many orders Brew Bar has taken today. That number does not belong to any single order; it belongs to the café as a whole. And the rule "no more than 20 cups per order" should never change while the program runs. static and final express exactly these two ideas.

The rule

static belongs to the class; final can be assigned only once.

A static member has one copy shared by the whole class, instead of one per object:

public class Order {
    public static final int MAX_CUPS = 20;
    private static int ordersToday = 0;

    private int cups;

    public Order(int cups) {
        if (cups > MAX_CUPS) {
            throw new IllegalArgumentException("At most " + MAX_CUPS + " cups");
        }
        this.cups = cups;
        ordersToday++;
    }

    public static int ordersToday() {
        return ordersToday;
    }
}

Every new Order(...) has its own cups but increases the single shared ordersToday. You reach static members through the class name: Order.ordersToday(), Order.MAX_CUPS. main is static for the same reason: the JVM calls it before any object exists.

A static method has no "this object", so it cannot use instance fields or methods directly. cups inside ordersToday() would not compile: whose cups?

final means "assigned exactly once":

  • A final variable cannot be reassigned. static final fields are constants, named in CAPITALS by convention.
  • A final method cannot be overridden by a subclass.
  • A final class cannot be extended at all. String is final.

One subtlety: final locks the variable, not the object it refers to. A final Drink house = new Drink("House", 250); can still have house.priceCents changed; you just cannot point house at another drink.

The shortcut

Ask of every field: "One per object, or one for everyone?" One for everyone → static. Then ask: "Should it ever change?" Never → final. Both answers together give a constant: static final.

Classifying Order's fieldsStep 1 of 4
  1. cups

    Each order has its own count, and it changes: an ordinary instance field.

Drills

Answer fast — trust the rule.

quick drill

Starting from zero, the program runs new Order(2); new Order(5); new Order(1);. What is Order.ordersToday()?

quick drill

What happens if ordersToday() (a static method) contains return cups;?

quick drill

final Drink house = new Drink("House", 250); Which line compiles?

quick drill

Why does class MyString extends String fail to compile?

Remember

  • static belongs to the class; final can be assigned only once.
  • Ask "one per object or one for everyone?" then "should it ever change?"
  • Traps: using instance fields in a static method, and thinking final freezes the object.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainOne counter or many?

A developer declares private int ordersToday = 0; (without static) in Order and increases it in the constructor. After three orders, every order reports 1. Explain why, how static fixes it, and whether ordersToday should also be final.