What Are Keywords in Java?

Keywords are words with a fixed, built-in meaning in the Java language. The compiler treats them specially — you cannot use them as variable names, method names, or class names. They are case-sensitive; class is a keyword, Class is not.

The full keyword list

abstract   assert      boolean    break       byte
case       catch       char       class       const*
continue   default     do         double      else
enum       extends     final      finally     float
for        goto*       if         implements  import
instanceof int         interface  long        native
new        package     private    protected   public
return     short       static     strictfp    super
switch     synchronized this       throw       throws
transient  try         void       volatile    while

* const and goto are reserved but unused. They are present so the compiler can give a useful error message if you accidentally type them.

Contextual keywords (newer Java)

These words have special meaning only in specific syntactic positions. You can still use them as identifiers elsewhere (though you probably shouldn't).

KeywordAddedContext
recordJava 16Class declaration: record Point(int x, int y) {}
sealedJava 17Class/interface: sealed interface Shape permits Circle, Rect
permitsJava 17After sealed to list allowed subtypes
non-sealedJava 17Re-opens a sealed class to arbitrary extension
yieldJava 14Inside a switch expression to produce a value
varJava 10Local variable type inference: var list = new ArrayList<>();

Reserved literals (not keywords, but still reserved)

true, false and null are reserved literals — they have special meaning but are technically not classified as keywords in the Java Language Specification. You still cannot use them as identifiers.

Why can't you use keywords as names?

int class = 5;    // compile error: 'class' is a keyword
int new   = 10;   // compile error: 'new' is a keyword
int Class = 5;    // fine: 'Class' is not a keyword (case matters)

The parser assigns a special token type to each keyword before it even tries to build a syntax tree. If a keyword appears where a name is expected, parsing fails immediately.

Category overview

  • Primitive types: boolean, byte, char, short, int, long, float, double
  • OOP: class, interface, extends, implements, new, this, super, abstract, final, enum
  • Access: public, protected, private
  • Control flow: if, else, switch, case, default, for, while, do, break, continue, return
  • Exceptions: try, catch, finally, throw, throws
  • Modifiers: static, synchronized, volatile, transient, native, strictfp