skip to content

Keywords & Reserved Words

The reserved words, the reserved literals true, false and null, the contextual keywords like var, record, sealed and yield, and the never-used goto and const. Interviewers like the contextual ones because they explain why you can still name a variable record.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

5

What are keywords (reserved words) in Java, and why can't you use them as identifiers?

level: juniorimportance: must knowfreq 70%

answer

  1. Reserved for the grammar, fixed meaning
  2. ~50 words; cannot be identifiers
  3. Compiler needs unambiguous parsing
  4. true/false/null are reserved literals, not keywords
  5. Convention: clazz instead of class

basics

~10 s

Keywords 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 s

Keywords 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 lines
java
int 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

for a junior

Knows keywords are reserved words with fixed meaning that can't be used as variable/method names, and can name common ones.

for a middle

Can explain why reservation is needed (unambiguous parsing) and groups keywords by purpose; knows true/false/null are reserved.

for a senior

Distinguishes reserved words from reserved literals and from ordinary library identifiers (String); knows the ~50 count and the unused goto/const.

for a principal

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)

context

open as a page

Which commonly-mistaken Java words are NOT keywords (e.g. String, System, main, length)? How do you tell a keyword from a library identifier?

level: juniorimportance: should knowfreq 40%

basics

~20 s

Words like String, System, main, args, and length are NOT keywords — they're class names, method names, or fields from the libraries. Keywords are the fixed grammar words like class, public, and void. You could even reuse the library names as your own (though it's a bad idea).

open as a page

What are the `goto` and `const` keywords in Java, and why are they reserved if they do nothing?

level: middleimportance: should knowfreq 45%

basics

~20 s

goto and const are reserved words in Java but they have no use — you cannot write working code with them. Java reserved them so they can't be variable names and to leave the door open and give clearer errors.

open as a page

What are contextual (restricted) keywords in Java, and how do they differ from regular reserved keywords? Give examples like var, yield, record, sealed, permits.

level: seniorimportance: should knowfreq 50%

basics

~10 s

Contextual keywords (like var, yield, record, sealed) only act as keywords in certain places. Everywhere else they're ordinary names, so old code that used them as variable names still compiles.

open as a page

Are `true`, `false`, and `null` keywords in Java? Where do they fit among reserved words?

level: middleimportance: nice to knowfreq 35%

basics

~10 s

Technically they are reserved literals, not keywords — true/false are boolean literals and null is the null literal. But like keywords, you cannot use them as names.

open as a page