Java Reference ยท Keywords and Statements
Java reserved words and keywords
The complete list of Java reserved words: keywords, literals, unused words, contextual keywords, and how to check them with the SourceVersion API.
Java reserves a set of words for the language itself. You cannot use them as names for variables, methods, classes, or packages. They fall into four groups: keywords with a fixed meaning everywhere, three literals (true, false, null), two reserved but unused words (goto, const), and contextual keywords that only act as keywords in certain places.
The list grows slowly and carefully. A new feature is introduced with a contextual keyword when possible, so that old programs which use that word as an identifier keep compiling.
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
The lists
Keywords (51 in current Java, counting _):
| Group | Words |
|---|---|
Primitive types and void |
boolean, byte, char, double, float, int, long, short, void |
| Control flow | if, else, switch, case, default, for, while, do, break, continue, return |
| Exceptions | try, catch, finally, throw, throws, assert |
| Classes and types | class, interface, enum, extends, implements, new, this, super, instanceof |
| Modifiers | public, protected, private, static, final, abstract, native, synchronized, transient, volatile, strictfp |
| Packages | package, import |
| Unused | goto, const |
| Underscore | _ (an unnamed variable or pattern since Java 22; before Java 9 it was a legal identifier) |
Literals: true, false, null (technically not keywords, but also not usable as identifiers).
Contextual (restricted) keywords: var, yield, record, sealed, non-sealed, permits, when, and the module words module, open, opens, requires, transitive, exports, to, uses, provides, with.
Usage notes
- Reserved vs contextual:
classcan never be an identifier;record,sealed,permits,when, and the module words can be used as variable or method names.var,yield, andrecord(plussealed,permits) cannot be used as type names, because they introduce syntax where a type name would appear. gotoandconstare reserved so that C-style code fails with a clear error, but they have no meaning. Usefinalfor constants and labeledbreak/continuefor escaping loops.- Literals:
true,false, andnullare case sensitive (Trueis a normal identifier, which is a classic mistake for Python programmers). non-sealedis the only hyphenated keyword, built fromnon,-,sealedas a single token only when both words are adjacent.- Case sensitivity: keywords are lowercase only.
ClassandCLASSare legal identifiers (though poor style). - Version history:
assertappeared in 1.4,enumin 5,varin 10 (restricted identifier),recordandyieldin 14 to 16,sealedandpermitsin 17,whenin 21, and_as the unnamed placeholder in 22. - Generated code: tools that generate Java from other languages must avoid these words; Jackson, JPA, and similar tools often add a suffix or use annotations to map a field called
classordefault. - Checking programmatically:
javax.lang.model.SourceVersion.isKeyword(CharSequence)andisName(CharSequence)answer the question for you, as the first example shows (isIdentifieronly checks the characters and accepts keywords).