Java Keywords
Java keywords are words that the language has claimed for its own. You cannot use them as variable names, class names, or method names — they are the building blocks of the language itself. Knowing them helps you write valid code and understand error messages like "expected identifier" when you accidentally name something class or return.
What Is a Keyword?
A keyword (also called a reserved word) is a word with a fixed meaning baked into the Java language specification. The compiler treats every keyword as a special token rather than a user-defined identifier.
int class = 5; // COMPILE ERROR — 'class' is a keyword
int myClass = 5; // Fine — not a keyword
Java currently has 67 reserved keywords (as of Java 21). Two of them — goto and const — are reserved but not used; they exist to prevent confusion for developers migrating from C/C++. Three special literals (true, false, null) behave like keywords and also cannot be used as identifiers.
Note: Keywords are always lowercase.
class,int, andwhileare keywords;Class,Int, andWhileare not (though using them as names would be confusing — follow the naming conventions).
Full Keyword Reference
The table below groups all 67 keywords by their role in the language.
Primitive Data Types
These eight keywords declare the built-in primitive types. See Data Types for sizes and ranges.
| Keyword | What it represents |
|---|---|
byte | 8-bit signed integer |
short | 16-bit signed integer |
int | 32-bit signed integer |
long | 64-bit signed integer |
float | 32-bit floating-point |
double | 64-bit floating-point |
char | 16-bit Unicode character |
boolean | true or false value |
int age = 30;
double price = 9.99;
boolean active = true;
char grade = 'A';
Class, Interface & Object Structure
These keywords define the shape and relationships of your types.
| Keyword | Purpose |
|---|---|
class | Declare a class |
interface | Declare an interface |
enum | Declare an enumeration |
record | Declare a record (Java 16+) |
extends | Inherit from a superclass |
implements | Implement one or more interfaces |
abstract | Mark a class or method as abstract |
new | Create an object instance |
this | Reference the current object |
super | Reference the parent class |
instanceof | Test an object’s type |
void | Declare a method with no return value |
public class Animal {
String name;
}
public class Dog extends Animal implements Runnable {
@Override
public void run() {
System.out.println(name + " runs!");
}
}
See Classes & Objects, Inheritance, and Interfaces for deep dives on these.
Access & Scope Modifiers
| Keyword | Meaning |
|---|---|
public | Accessible from anywhere |
private | Accessible only within the declaring class |
protected | Accessible within the package and subclasses |
static | Belongs to the class, not to any instance |
final | Cannot be extended / overridden / reassigned |
abstract | Must be implemented by a subclass |
synchronized | One thread at a time (used in concurrency) |
volatile | Always read from main memory (used in concurrency) |
transient | Exclude field from serialization |
native | Method implemented in native code (C/C++) |
strictfp | Enforce strict floating-point precision (deprecated in Java 17) |
public class BankAccount {
private double balance; // private: hidden from outside
public static final double MIN_BALANCE = 0.0; // public + static + final
public synchronized void deposit(double amount) { // synchronized: thread-safe
balance += amount;
}
}
The static keyword, final keyword, and access modifiers each have their own full pages.
Control Flow
These keywords direct the execution path through your program.
| Keyword | Purpose |
|---|---|
if | Conditional branch |
else | Alternative branch |
switch | Multi-branch selector |
case | A branch within switch |
default | Fallback branch in switch (also used in interfaces) |
for | Counted loop |
while | Condition-tested loop |
do | Body-first loop (do-while) |
break | Exit a loop or switch |
continue | Skip to the next loop iteration |
return | Exit a method and optionally return a value |
public class ControlDemo {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) continue; // skip 3
if (i == 5) break; // stop at 5
System.out.println(i);
}
}
}
Output:
1
2
4
Explore these in detail: if-else, switch, for loop, while loop, break, continue.
Exception Handling
| Keyword | Purpose |
|---|---|
try | Wrap code that might throw an exception |
catch | Handle a thrown exception |
finally | Always-runs block after try/catch |
throw | Manually throw an exception |
throws | Declare checked exceptions a method may throw |
public class SafeDivide {
public static int divide(int a, int b) throws ArithmeticException {
return a / b;
}
public static void main(String[] args) {
try {
System.out.println(divide(10, 0));
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero: " + e.getMessage());
} finally {
System.out.println("Done.");
}
}
}
Output:
Cannot divide by zero: / by zero
Done.
See Exception Handling for the full picture.
Package & Import
| Keyword | Purpose |
|---|---|
package | Declare the package this file belongs to |
import | Bring a class or package into scope |
package com.devcraftly.demo;
import java.util.ArrayList;
import java.util.List;
public class Example {
List<String> items = new ArrayList<>();
}
Learn more in Packages.
Type-Checking & Casting
Object obj = "Hello";
if (obj instanceof String s) { // pattern matching (Java 16+)
System.out.println(s.toUpperCase()); // HELLO
}
The instanceof keyword, including its modern pattern-matching form, is covered on the instanceof Operator page.
Module System (Java 9+)
Java 9 introduced the module system with a handful of context-sensitive keywords — they are only reserved inside module-info.java files and can still be used as identifiers elsewhere:
module, requires, exports, opens, uses, provides, with, to, transitive, open
// module-info.java
module com.devcraftly.app {
requires java.base;
exports com.devcraftly.api;
}
See Java 9 Modules for details.
Unused / Reserved-Only Keywords
| Keyword | Status |
|---|---|
goto | Reserved but not used — left over from C |
const | Reserved but not used — use final instead |
Attempting to use either causes a compile error, even though the compiler itself never does anything with them.
Special Literals
Three tokens behave exactly like keywords — they are case-sensitive and cannot be identifiers:
| Literal | Meaning |
|---|---|
true | Boolean true value |
false | Boolean false value |
null | Absence of an object reference |
boolean isReady = true;
String name = null; // no object assigned yet
System.out.println(isReady); // true
System.out.println(name); // null
Warning:
True,False, andNull(capital first letter) are NOT keywords — they are valid (though terrible) variable names. Stick to lowercase as the spec requires.
Quick Summary: All 67 Keywords at a Glance
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
record return short static strictfp
super switch synchronized this throw
throws transient try var** void
volatile while
* = reserved but not used
** = var is a reserved type name since Java 10, not a traditional keyword — it can still be used as a method or package name (though you shouldn’t)
Under the Hood
When the Java compiler (javac) lexes your source file, it reads the character stream and produces tokens. Keywords are identified during this tokenization phase — before parsing even begins. The lexer compares each identifier-shaped token against a fixed lookup table; if it matches, the token is tagged as a keyword type rather than an identifier type.
This means the compiler catches a misused keyword immediately and precisely: the error points to the exact column where you wrote class as a variable name, before any semantic analysis occurs.
Why can’t you use keywords as identifiers?
The grammar rules for Java would become ambiguous. For example, if if were also a valid variable name, the parser could not determine whether if (x) starts a conditional statement or an expression involving a variable called if. Reserving these tokens removes the ambiguity entirely.
Context-sensitive keywords (like record, sealed, permits, yield) work differently — they were introduced later and made context-sensitive to preserve backward compatibility with existing code that may already use those names as identifiers.
Tip: If you ever get a compiler error about an unexpected token and you are not sure why, check whether you accidentally used a keyword as an identifier. Modern IDEs highlight reserved words in a distinct color to prevent this.
Related Topics
- Variables — learn the rules for naming identifiers (which must avoid all keywords)
- Data Types — the eight primitive-type keywords explained in depth
- Control Statements —
if,switch,for,while,break,continuein action - Access Modifiers —
public,private,protected, and package-private explained - static Keyword — one of the most powerful and commonly misunderstood keywords
- final Keyword — use
finalfor constants, immutable variables, and sealed hierarchies