Java Reserved Keywords — What They Are and Why You Can't Use Them
Fast recall
Quick revision
Core answer
class, if, and return, so they cannot be ordinary identifiers. The exact list depends on the Java language version, and Java also has reserved literals (true, false, null) and restricted identifiers such as var, record, sealed, and yield with context-specific rules. Avoid quoting one timeless keyword count.Basic Java language literacy and awareness of syntax rules every Java developer must know from day one
Not knowing that true, false, and null are technically literals (not keywords) but are equally reserved, or trying to use keywords like 'class' or 'int' as names
Mention that var and yield are context-sensitive keywords — they can be names in some contexts but act as keywords in others, unlike strict keywords which are always reserved
Interview-ready guide
Interview answer
Java keywords are words reserved by the language grammar, such as class, if, return, and static, so they cannot be used as ordinary identifiers. const and goto are reserved even though Java does not implement those operations.
For example, int class = 1; does not compile, while int classification = 1; is valid. true, false, and null are literals, not keywords, but they are also unavailable as names. Forms such as var, record, sealed, and yield have context- and version-specific rules. I avoid quoting one permanent keyword count because Java versions and teaching sources classify contextual forms differently.
Java Reserved Keywords — What They Are and Why You Can't Use Them
Direct Answer
Java keywords are reserved language words such as class, if, and return, so they cannot be ordinary identifiers. The exact list depends on the Java language version, and Java also has reserved literals (true, false, null) and restricted identifiers such as var, record, sealed, and yield with context-specific rules. Avoid quoting one timeless keyword count.
Code Example
public class KeywordDemo {
private int count = 10; // valid
private boolean isActive = true; // 'isActive' valid; 'true' is a literal
void demo() {
var orders = java.util.List.of("A", "B"); // 'var' as a keyword (type inference)
// int class = 5; // ERROR: keyword
// const int x = 5; // ERROR: 'const' reserved but unused — use 'final'
System.out.println(count + orders.size());
}
}
This compiles cleanly on Java 17. Uncomment either error line and the build breaks instantly — proof that the rule is enforced at compile time, long before the program ever runs. The var orders line shows the same word acting as a keyword, while the commented const line shows a word that is reserved yet does nothing.
Common Mistakes & Pitfalls
- Counting unlike categories together: keywords, contextual forms, restricted identifiers, and literals are not one category.
- Calling true, false, and null keywords: the JLS classifies them as literals.
- Using const for a Java constant:
constis reserved but unused; usefinalfor a variable that cannot be reassigned. - Assuming a rule is timeless: newer Java versions add grammar forms.
- Treating contextual words as unrestricted everywhere: legality depends on their source position.
Check your understanding
Answer these without looking back at the article.
- Are true, false, and null keywords in Java?
- What are goto and const in Java?
- What is a context-sensitive keyword in Java?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.