Keywords: Java's Reserved Vocabulary

Keywords: Java's Reserved Vocabulary in Java: Keywords are the roughly 50 words Java reserves for itself.

Concept Explanation

Keywords are the roughly 50 words Java reserves for itself. They are the fixed grammar of the language — you can never use one as a variable or class name. You have already used several without naming them: public, class, static, void, int.

You do not need to memorise the list. Recognising the groups is enough, and the rest arrive naturally as you learn each feature.


The Groups That Matter

Primitive types — byte, short, int, long, float, double, char, boolean

Access and modifiers — public, private, protected, static, final, abstract, synchronized, volatile, transient, native, strictfp

Control flow — if, else, switch, case, default, for, while, do, break, continue, return, yield

Classes and objects — class, interface, enum, extends, implements, new, this, super, instanceof, record

Errors — try, catch, finally, throw, throws, assert

Organisation — package, import, module

public class Bank {          // public, class
    private final int id;    // private, final, int

    public Bank(int id) {
        this.id = id;        // this
    }

    public static void main(String[] args) {   // static, void
        for (int i = 0; i < 3; i++) {          // for, int
            if (i == 1) continue;              // if, continue
            System.out.println(i);
        }
    }
}

Two Curiosities

  • goto and const are reserved but unused. Java claims them so nobody uses them, yet gives them no meaning.
  • true, false, and null feel like keywords but are technically literals. You still cannot use them as names.

Contextual Keywords

Newer additions such as var, record, sealed, permits, and yield are only special in certain positions. int var = 5; actually compiles — though writing it is a poor idea.


Common Mistakes

  1. Naming a variable after a keyword: int class = 5; fails immediately. So do new, return, double.
  2. Assuming String is a keyword: It is a class from the standard library, which is why it is capitalised.
  3. Confusing final with finally and finalize: Three unrelated things — a constant modifier, an exception block, and a deprecated cleanup hook.

Best Practices

  • Let the IDE catch it: Keywords are colour-coded, so an accidental use is visible instantly.
  • Learn them feature by feature: You will meet synchronized when you reach threads. No need to rush.
  • Never work around a clash: If class is the natural name, use courseClass — not clazz or klass.

Practice Exercises

  1. The Keyword Hunt: Take the Bank example above and list every keyword you can find, with one sentence on each.
  2. The Clash Test: Try int new = 5; and int native = 2;. Read the compiler errors carefully.
  3. The Odd Ones Out: Explain why true, false, and null are literals rather than keywords, and why it makes no practical difference to you.