What are keywords (reserved words) in Java, and why can't you use them as identifiers?
answer
- Reserved for the grammar, fixed meaning
- ~50 words; cannot be identifiers
- Compiler needs unambiguous parsing
- true/false/null are reserved literals, not keywords
- Convention: clazz instead of class
basics
~10 sKeywords are words Java reserves for its own grammar (like class, if, return, int). The compiler treats them specially, so you can't use them to name your own variables, methods, or classes.
solid answer
~40 sKeywords are a fixed set of words the Java language reserves for its syntax: control flow (if, else, for, while), declarations (class, interface, void, int), modifiers (public, static, final), and more. The compiler matches them while parsing, so they have a fixed meaning and cannot serve as identifiers (names you invent for variables, methods, classes, packages). Writing `int class = 5;` is a compile error because `class` is reserved. Java has around 50 such words. Note that the literals `true`, `false`, and `null` are reserved separately (they are reserved literals, not technically keywords) but are equally off-limits as names. This reservation lets the compiler unambiguously tell grammar from your names without guessing.
code
java · 3 linesint total = 5; // OK: 'total' is an identifier you chose
// int class = 5; // compile error: 'class' is a reserved word
String clazz = "X"; // convention: use 'clazz' when you wanted 'class'go deeper
Knows keywords are reserved words with fixed meaning that can't be used as variable/method names, and can name common ones.
Can explain why reservation is needed (unambiguous parsing) and groups keywords by purpose; knows true/false/null are reserved.
Distinguishes reserved words from reserved literals and from ordinary library identifiers (String); knows the ~50 count and the unused goto/const.
Can discuss how reservation interacts with grammar/lexer design and why the language committee avoids adding hard keywords (backward compatibility), preferring contextual keywords.
## What an identifier is An **identifier** is any name *you* invent in code: the name of a variable, method, class, interface, package, or label. For example in `int total = 5;`, `total` is an identifier you chose. ## What a keyword is A **keyword** (also called a **reserved word**) is a word that the Java language itself claims for its grammar. Examples: `class`, `public`, `static`, `void`, `int`, `if`, `else`, `for`, `while`, `return`, `new`, `try`, `catch`, `extends`, `implements`. These words have a **fixed, built-in meaning**. ## Why they can't be identifiers When the Java **compiler** reads your source, it does **lexing** (splitting text into tokens) and **parsing** (matching tokens against the language grammar). If `class` could be both a keyword *and* a name you chose, the compiler would face ambiguity: in `class x = class;` it couldn't tell which `class` starts a class declaration and which is a value. To keep the grammar unambiguous and simple, Java **forbids** using any reserved word as an identifier. So `int new = 3;` and `String for = "a";` are compile errors. ## How many, and what kinds Java has roughly **50** reserved words. They group into: - **Control flow:** `if`, `else`, `switch`, `case`, `default`, `for`, `while`, `do`, `break`, `continue`, `return`. - **Types / declarations:** `class`, `interface`, `enum`, `void`, plus the eight primitives `boolean`, `byte`, `char`, `short`, `int`, `long`, `float`, `double`. - **Modifiers:** `public`, `protected`, `private`, `static`, `final`, `abstract`, `synchronized`, `transient`, `volatile`, `native`, `strictfp`. - **OOP / inheritance:** `extends`, `implements`, `new`, `this`, `super`, `instanceof`. - **Exceptions:** `try`, `catch`, `finally`, `throw`, `throws`, `assert`. - **Packaging:** `package`, `import`. - **Reserved-but-unused:** `goto` and `const` (reserved so they can never be names, but they do nothing). ## Reserved literals The three values `true`, `false`, and `null` are **reserved literals**, not technically keywords in the JLS sense, but they are equally protected — you cannot name a variable `null`. They behave like keywords for practical purposes. ## The practical takeaway If the compiler ever complains about an unexpected token where you put a name, check whether you accidentally used a reserved word. The fix is to pick a different name (e.g. `clazz` instead of `class`, a long-standing convention).
- Are true, false, and null keywords?Technically no — the JLS calls them reserved literals (boolean literals and the null literal). But they are reserved just like keywords, so you cannot use them as identifiers either.
- How many keywords does Java have?Around 50 reserved words, including the two unused ones (goto, const). The exact count shifts slightly as the language adds features, but ~50 is the right ballpark.
Keywords are like the road signs of a language: STOP always means stop. You can't repaint a STOP sign to mean your street's name, or drivers (the compiler) would be confused.
saying these in an interview costs you the question
- Claiming you can use a keyword as an identifier if you change its case (Java is case-sensitive, but Class/CLASS being usable doesn't mean class is — only the exact reserved spelling is blocked)
- Saying true/false/null are keywords without noting they are technically reserved literals
- Confusing keywords (reserved by grammar) with library names like String or System (those are just classes and CAN be shadowed/reused as identifiers, though it's a bad idea)