☕ Java

Java Reserved Words

Reserved words are identifiers you cannot use in your Java code — they're claimed by the language itself. This includes keywords, literals, and a handful of reserved-but-unused words. Knowing them prevents naming conflicts and gives you a complete picture of Java's vocabulary.

Reserved Words vs Keywords

These two terms are often used interchangeably, but they have a precise distinction in Java: Keywords are reserved words that have specific meaning in Java's grammar — class, if, return, static, and so on. The compiler uses them to parse your code. Reserved words is the broader category. It includes keywords plus two special literals (true, false, null) and two words reserved for future use (goto, const) that currently don't compile as either identifiers or functional keywords. The practical rule is the same for both: you cannot use any of them as identifiers — variable names, class names, method names, or package names.

The Three Categories of Reserved Words

Java's reserved words fall into three groups:
Java
// ── Category 1: Keywords (active, have meaning in Java grammar) ──

// Primitive types:
byte short int long float double char boolean

// Control flow:
if else switch case default
for while do break continue return

// Class structure:
class interface enum extends implements
abstract final static
new this super null instanceof

// Access modifiers:
public protected private

// Exception handling:
try catch finally throw throws

// Package system:
package import

// Memory / threading:
synchronized volatile transient native strictfp

// Module system (Java 9+):
// 'module', 'requires', 'exports', 'opens', 'uses', 'provides', 'with', 'to'
// Note: these are context-sensitive keywords — valid as identifiers outside module-info.java

// ── Category 2: Reserved Literals (specific values, not assignable) ──
true    // boolean literal
false   // boolean literal
null    // reference type literal — absence of an object

// ── Category 3: Reserved But Unused ──
goto    // reserved, does nothing, can't be used as identifier
const   // reserved, does nothing, can't be used as identifier

true, false, and null — Literals, Not Keywords

Technically, true, false, and null are not keywords in the Java Language Specification — they're called "boolean literals" and "null literal" respectively. But they behave exactly like reserved words: you can't use them as identifiers.
Java
// These are literals — they represent specific values:
boolean isReady = true;
boolean isDone = false;
String name = null;

// They are NOT keywords — but you still can't use them as names:
int true = 1;      // compile error: illegal start of expression
String null = "";  // compile error: illegal start of expression
boolean false;     // compile error: illegal start of expression

// null is the default value for all reference types:
String s;           // s is null by default (if it's a field)
String[] arr = new String[3];  // arr[0], arr[1], arr[2] are all null

// Checking for null:
if (s != null) {
    System.out.println(s.length());  // safe — won't NPE
}

// Java 14+ — null in switch (pattern matching):
switch (obj) {
    case null -> System.out.println("null value");
    case String s -> System.out.println("String: " + s);
}

Context-Sensitive Keywords (Java 9+)

Java 9 introduced the module system, which added a set of context-sensitive keywords — words that are reserved only in specific contexts, but remain valid as identifiers elsewhere. This was done to maintain backward compatibility with existing code that may use these words as variable or method names.
Java
// These are keywords ONLY inside module-info.java:
module com.example.app {
    requires java.base;
    requires transitive java.logging;
    exports com.example.api;
    exports com.example.internal to com.example.tests;
    opens com.example.model;
    uses com.example.spi.Service;
    provides com.example.spi.Service with com.example.ServiceImpl;
}

// Outside module-info.java, these are valid identifiers:
String module = "payment";        // valid — 'module' is not a keyword here
int requires = 5;                 // valid
void exports() { }                // valid

// Same for newer context-sensitive keywords:
// 'record' (Java 16), 'sealed' (Java 17), 'permits' (Java 17), 'yield' (Java 14)
record Point(int x, int y) { }   // 'record' as a type declaration keyword
int record = 100;                 // still valid as an identifier (for compatibility)

Complete Reserved Word Reference

All reserved words alphabetically — everything you cannot use as an identifier:
Java
abstract    assert      boolean     break       byte
case        catch       char        class       const*
continue    default     do          double      else
enum        extends     falsefinal       finally
float       for         goto*       if          implements
import      instanceof  int         interface   long
native      new         nullpackage     private
protected   public      return      short       static
strictfp    super       switch      synchronized this
throw       throws      transient   truetry
var‡        void        volatile    while

// * reserved but unused (goto, const)
// † technically literals, not keywords (true, false, null)
// ‡ var is a special case — it's a reserved type name in local variable
//   type inference (Java 10+), but NOT a keyword. You can still use
//   'var' as a method name or package name (just not as a type name).

// var in action:
var list = new ArrayList<String>();  // compiler infers ArrayList<String>
var name = "Alice";                  // compiler infers String
var count = 42;                      // compiler infers int