Java Keywords: Reserved Words You Must Know

Introduction

This chapter introduces Java keywords—reserved words with special meaning in the language. Keywords define syntax and control flow. Using them correctly keeps code valid; misusing them (for example as variable names) causes compile errors.

Some keywords (break, continue, return) are easier to understand inside loops—see Loop Statements for full examples.

Prerequisites

  • Variables and Operators
  • Basic reading of if and for shapes (details in upcoming chapters)

What Is a Keyword

A keyword is a word Java reserves for language rules. You cannot use keywords as identifiers (class names, variable names, method names).

java
// Valid
String userName = "Alice";
 
// Invalid — 'class' is a keyword
// int class = 10;

Literals true, false, and null are not keywords in the JLS sense but are reserved and cannot be used as identifiers either.

1) Common Keyword Groups

You do not need to memorize every keyword on day one. Learn groups you will use immediately.

Flow Control

KeywordRole
if, elsebranching
switch, case, defaultmulti-way branch
for, while, doloops
break, continueloop control
returnexit a method

Preview:

java
if (score >= 60) {
    System.out.println("Pass");
} else {
    System.out.println("Fail");
}

break / continue / return in loops: Loop Statements.

Types and Declarations

boolean, byte, short, int, long, float, double, char, void, var (local type inference), enum, record (Java 16+), class, interface, extends, implements

java
int count = 0;
boolean active = true;

Access and Class Structure

public, private, protected, static, final, abstract, sealed, non-sealed, permits, this, super, new, instanceof

You will use many of these heavily in OOP chapters later.

Modules and Packages

package, import, module, requires, exports, opens

package / import appear early when projects grow beyond one file.

Exceptions

try, catch, finally, throw, throws

Covered in Exception Handling.

Legacy / Specialized (know they exist)

synchronized, volatile, transient, native, strictfp, assert

Not needed for every beginner program; appear in concurrency and advanced APIs.

Reserved but Unused in Java

const, goto — reserved for compatibility; do not use.

2) Full Keyword List (Java 21)

Java keywords include:

abstract, assert, boolean, break, byte, case, catch, char, class, continue, default, do, double, else, enum, extends, final, finally, float, for, 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

Contextual keywords (restricted meaning in certain places): var, record, yield, sealed, permits, non-sealed, module, exports, opens, requires, to, with, when

Tip

Best Practice

Let your IDE highlight keywords. If a name turns orange or you get “<identifier> expected,” you may have used a reserved word.

3) Keywords vs Other Reserved Names

KindExampleNotes
Keywordif, class, returnsyntax
Literaltrue, false, nullfixed values
Built-in typeint, doubleprimitives
Standard librarySystem, Stringclasses—not keywords; valid only with correct capitalization

System is a class name you can reference; system as a variable name is allowed (but confusing—avoid it).

4) Why Keywords Matter in Real Code

Every Java program uses keywords for structure:

  • if / switch for decisions → Conditionals
  • for / while for repetition → Loops
  • class / public / return for types and methods → OOP chapters
  • try / catch for robustness → later chapters

If you treat keywords as normal names, the compiler stops you—which is safer than silent confusion.

Common Beginner Mistakes

Mistake 1: Keyword as Variable Name

java
// int class = 5;     // error
// String public = "x"; // error

Fix: rename (className, publicId).

Mistake 2: Confusing == with .equals() for Strings

Not a keyword issue, but very common next to if:

java
// if (name == "Alice")  // often wrong for String content
if ("Alice".equals(name)) { }

Mistake 3: Misspelling else if as elseif

Java uses two words: else if.

java
if (score >= 90) {
} else if (score >= 80) {
} else {
}

Warning

Do not force-memorize every keyword at once. Master if, else, for, while, return, class, public, and static first; expand as chapters introduce new features.

Mini Practice

Preview of conditionals (full chapter next):

java
import java.util.Scanner;
 
public class GradePreview {
    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.print("Enter your score: ");
            double score = scanner.nextDouble();
 
            if (score >= 90) {
                System.out.println("Grade: A");
            } else if (score >= 80) {
                System.out.println("Grade: B");
            } else if (score >= 70) {
                System.out.println("Grade: C");
            } else {
                System.out.println("Grade: D");
            }
        }
    }
}

What’s Next

Conditional Statements for if, switch, and the ternary operator.

FAQ

Do Java keywords change between versions?

Occasionally new contextual keywords appear (var, record, sealed). Core keywords like if and class stay stable.

Is String a keyword?

No. String is a class in java.lang.

Should I memorize all keywords now?

No. Learn them through usage in conditionals, loops, and classes.

How do I avoid keyword conflicts?

Use descriptive camelCase names: className, forCount, isActive.

Where are break and continue explained in depth?

Loop Statements, with switch fall-through notes in Conditionals.