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
gotoandconstare reserved but unused. Java claims them so nobody uses them, yet gives them no meaning.true,false, andnullfeel 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
- Naming a variable after a keyword:
int class = 5;fails immediately. So donew,return,double. - Assuming
Stringis a keyword: It is a class from the standard library, which is why it is capitalised. - Confusing
finalwithfinallyandfinalize: 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
synchronizedwhen you reach threads. No need to rush. - Never work around a clash: If
classis the natural name, usecourseClass— notclazzorklass.
Practice Exercises
- The Keyword Hunt: Take the
Bankexample above and list every keyword you can find, with one sentence on each. - The Clash Test: Try
int new = 5;andint native = 2;. Read the compiler errors carefully. - The Odd Ones Out: Explain why
true,false, andnullare literals rather than keywords, and why it makes no practical difference to you.