Java keywords are words the Java language reserves for its own use — like class, public, static and if. Because the compiler gives them a fixed meaning, you can't use them as names for variables, methods or classes. Java 21 has 51 reserved keywords, plus a set of contextual keywords such as var and record that only act as keywords in certain places.
- The complete list of all 51 Java keywords and what each one does
- The difference between reserved keywords, contextual keywords and literals (
true,false,null) - Where
var,recordandyieldcan and can't be used - The compiler errors you get when you use a keyword as a name — and how to fix them
How Many Keywords Are There in Java?
There are 51 reserved keywords in current Java (Java 9 and later). Java 8 had 50; Java 9 added the underscore _. On top of those, Java has 16 contextual keywords (also called restricted keywords or restricted identifiers) that behave like keywords only in a specific context. Every word in the tables below was checked against the Java 21 compiler.
| Kind | Count | Can you use it as a variable name? |
|---|---|---|
| Reserved keywords | 51 | No — compile error |
| Contextual keywords | 16 | Yes, in most places (see below) |
Literals: true, false, null | 3 | No — they are not keywords, but they are reserved values |
List of All 51 Java Keywords
Grouped by what they do, so they're easier to remember:
| Keyword | What it does | Group |
|---|---|---|
public | Visible everywhere | Access modifiers |
protected | Visible in the same package and in subclasses | Access modifiers |
private | Visible only inside the same class | Access modifiers |
class | Declares a class | Classes, interfaces and objects |
interface | Declares an interface | Classes, interfaces and objects |
enum | Declares a fixed set of constants | Classes, interfaces and objects |
extends | Inherits from a class (or interface from interface) | Classes, interfaces and objects |
implements | A class takes on an interface | Classes, interfaces and objects |
new | Creates an object | Classes, interfaces and objects |
this | The current object | Classes, interfaces and objects |
super | The parent class (its methods, fields or constructor) | Classes, interfaces and objects |
instanceof | Checks an object's type | Classes, interfaces and objects |
abstract | Class or method without a full implementation | Classes, interfaces and objects |
package | Declares the package of the file | Classes, interfaces and objects |
import | Uses classes from another package | Classes, interfaces and objects |
byte | 8-bit whole number | Primitive types and void |
short | 16-bit whole number | Primitive types and void |
int | 32-bit whole number | Primitive types and void |
long | 64-bit whole number | Primitive types and void |
float | 32-bit decimal number | Primitive types and void |
double | 64-bit decimal number | Primitive types and void |
char | A single 16-bit character | Primitive types and void |
boolean | true or false | Primitive types and void |
void | A method returns nothing | Primitive types and void |
if | Runs code when a condition is true | Flow control |
else | Runs code when the if condition is false | Flow control |
switch | Chooses a branch by value | Flow control |
case | One branch of a switch | Flow control |
default | Fallback branch of a switch; default method in an interface | Flow control |
for | Loop with a counter or over a collection | Flow control |
while | Loop while a condition is true | Flow control |
do | Loop that runs at least once (do … while) | Flow control |
break | Leaves a loop or switch | Flow control |
continue | Skips to the next loop iteration | Flow control |
return | Leaves a method, optionally with a value | Flow control |
try | Starts a block that may throw an exception | Exception handling |
catch | Handles an exception | Exception handling |
finally | Always runs after try/catch | Exception handling |
throw | Throws an exception | Exception handling |
throws | Declares exceptions a method may throw | Exception handling |
assert | Checks a condition while testing (enabled with -ea) | Exception handling |
static | Belongs to the class, not to objects | Modifiers |
final | Cannot be changed, overridden or extended | Modifiers |
synchronized | Only one thread at a time | Modifiers |
volatile | Every thread sees the latest value | Modifiers |
transient | Skipped when an object is serialized | Modifiers |
native | Implemented in another language (C/C++) | Modifiers |
strictfp | Strict floating-point rules (no effect since Java 17) | Modifiers |
goto | Reserved; using it is a compile error | Reserved but not used |
const | Reserved; use final instead | Reserved but not used |
_ | Underscore — a keyword since Java 9; unnamed variables in newer Java | Reserved but not used |
Contextual Keywords: var, record, yield and More
These words are only keywords in a particular place, so older code that used them as names still compiles. The compiler confirms all 16 can be used as variable names — but five of them (var, yield, record, sealed, permits) cannot be used as class or type names.
| Contextual keyword | Where it acts as a keyword | Usable as a class name? |
|---|---|---|
var | Local variable type inference: var list = new ArrayList<String>(); (Java 10+) | No |
record | Declares a record class: record User(String name) {} (Java 16+) | No |
yield | Returns a value from a switch expression (Java 14+) | No |
sealed, permits, non-sealed | Sealed classes that limit which classes may extend them (Java 17+) | sealed/permits: No |
when | Guards in pattern-matching switch: case String s when s.isEmpty() (Java 21) | Yes |
module, requires, exports, opens, open, to, uses, provides, with, transitive | Only inside module-info.java (Java 9 modules) | Yes |
Java Keywords Example Program
This small program uses more than 25 keywords — each one is marked in a comment:
public class KeywordsDemo {
enum Level { BEGINNER, ADVANCED } // enum
interface Greeter { String greet(String name); } // interface
static final int MAX_TESTS = 3; // static, final, int
public static void main(String[] args) { // public, static, void
int passed = 0;
for (int i = 1; i <= MAX_TESTS; i++) { // for
boolean ok = (i % 2 != 0); // boolean
if (ok) { // if / else
passed++;
} else {
continue; // continue
}
}
Level level = (passed >= 2) ? Level.ADVANCED : Level.BEGINNER;
Greeter g = name -> "Hello, " + name;
try { // try / catch / finally
System.out.println(g.greet("tester"));
System.out.println("Passed " + passed + " of " + MAX_TESTS + " -> " + level);
if (passed < 0) throw new IllegalStateException("impossible"); // throw, new
} catch (IllegalStateException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("Done");
}
}
}
Output
Hello, tester
Passed 2 of 3 -> ADVANCED
Done
How it works: enum and interface declare types; static final makes a constant; the for loop uses if/else and continue; and try/catch/finally handles errors — finally runs every time, which is why "Done" is always printed.
Common Mistakes
- Using a keyword as a variable name. The error message doesn't say "keyword", which confuses beginners:
int class = 5;
Fix: choose another name such asBad.java:3: error: not a statement int class = 5; ^ Bad.java:3: error: ';' expectedclassNameorclazz. - Thinking Java keywords are case-insensitive. Keywords are all lowercase, so
Class,MainorStringare not keywords —int Class = 5;compiles (though it's bad style). - Calling
true,falseornullkeywords. They're literals. You still can't use them as names, but in interviews say "literals", not keywords. - Thinking
mainorStringis a keyword.mainis just a method name the JVM looks for, andStringis a class injava.lang. - Trying to use
gotoorconst. Both are reserved but unused — the compiler rejects them. Use labelledbreak/continueinstead ofgoto, andfinalinstead ofconst.
Practice Exercises
- Without looking, write down the 8 primitive-type keywords. Check them against the table.
- Write a class that uses
static,final,switch,case,defaultandbreak, and predict its output before running it. - Interview question: explain the difference between
final,finallyandfinalize()(hint: only two of them are keywords).
Key Takeaways
- Java 21 has 51 reserved keywords (including
_) and 16 contextual keywords. true,falseandnullare literals, not keywords — but they are still reserved.var,yield,record,sealedandpermitscan be variable names but not class names.
📚 Official documentation: Java Language Specification: Keywords (Java 21) · dev.java: Learn Java
Frequently Asked Questions
How many keywords are there in Java?
Java has 51 reserved keywords (Java 9 and later, including the underscore _) plus 16 contextual keywords such as var, record and yield. Java 8 had 50 reserved keywords.
Are true, false and null keywords in Java?
No. They are literals. You still cannot use them as names for variables, methods or classes.
Is main a keyword in Java?
No. main is an ordinary method name that the JVM looks for to start a program. String is a class, not a keyword, either.
Is var a keyword in Java?
var is a contextual keyword (a reserved type name) added in Java 10. You can still name a variable var, but you cannot name a class var.
Why are goto and const reserved in Java?
They were reserved so they could never be used by mistake. Neither has any meaning in Java, and the compiler rejects both.