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).
| Keyword | Added | Context |
|---|---|---|
record | Java 16 | Class declaration: record Point(int x, int y) {} |
sealed | Java 17 | Class/interface: sealed interface Shape permits Circle, Rect |
permits | Java 17 | After sealed to list allowed subtypes |
non-sealed | Java 17 | Re-opens a sealed class to arbitrary extension |
yield | Java 14 | Inside a switch expression to produce a value |
var | Java 10 | Local 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