Skip to content
Tutorials Center
My learning

Topics

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

Java Asking the compiler API about keywords

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.

Java Contextual keywords are still valid names

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: class can never be an identifier; record, sealed, permits, when, and the module words can be used as variable or method names. var, yield, and record (plus sealed, permits) cannot be used as type names, because they introduce syntax where a type name would appear.
  • goto and const are reserved so that C-style code fails with a clear error, but they have no meaning. Use final for constants and labeled break/continue for escaping loops.
  • Literals: true, false, and null are case sensitive (True is a normal identifier, which is a classic mistake for Python programmers).
  • non-sealed is the only hyphenated keyword, built from non, -, sealed as a single token only when both words are adjacent.
  • Case sensitivity: keywords are lowercase only. Class and CLASS are legal identifiers (though poor style).
  • Version history: assert appeared in 1.4, enum in 5, var in 10 (restricted identifier), record and yield in 14 to 16, sealed and permits in 17, when in 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 class or default.
  • Checking programmatically: javax.lang.model.SourceVersion.isKeyword(CharSequence) and isName(CharSequence) answer the question for you, as the first example shows (isIdentifier only checks the characters and accepts keywords).