Keywords in Java – Complete List with Examples
Keywords are words with a special meaning in the Java language, such as class, if, return and static. The compiler reserves them, so you cannot use them as names for variables, methods or classes.
This tutorial lists every Java keyword grouped by purpose, explains the newer contextual keywords such as var, record and yield, and shows what happens when you misuse a keyword.
1. What Are Keywords?
Think of keywords as the grammar words of Java. Just as "if", "while" and "return" have fixed meanings in instructions, Java's keywords tell the compiler how to read your code: class starts a class, int names a type, for starts a loop.
- All keywords are written in lowercase.
- Reserved keywords can never be used as identifiers.
true,falseandnulllook like keywords but are literals; they are also reserved.- Contextual keywords are special only in certain places and can otherwise be used as names.
2. Keywords in a Real Program
A small program already uses many keywords. The comments mark them:
import java.util.List;
public class KeywordsInAction { // public, class
private static final int LIMIT = 3; // private, static, final, int
interface Shape { double area(); } // interface, double
record Square(double side) implements Shape { // record (contextual), implements
public double area() { return side * side; } // return
}
public static void main(String[] args) { // void
List<Shape> shapes = List.of(new Square(2), new Square(3), new Square(5)); // new
int count = 0;
for (Shape s : shapes) { // for
if (count >= LIMIT) break; // if, break
if (s instanceof Square sq && sq.side() > 2) { // instanceof
System.out.println("Big square, area " + sq.area());
} else { // else
System.out.println("Small square, area " + s.area());
}
count++;
}
try { // try
throw new IllegalStateException("demo"); // throw
} catch (IllegalStateException e) { // catch
System.out.println("Caught: " + e.getMessage());
} finally { // finally
System.out.println("finally always runs");
}
}
}
Small square, area 4.0 Big square, area 9.0 Big square, area 25.0 Caught: demo finally always runs
3. Reserved Keywords Grouped by Purpose
Java has 51 reserved keywords (including _). Here they are grouped by what they do:
| Group | Keywords |
|---|---|
| Primitive types | boolean, byte, char, short, int, long, float, double, void |
| Classes, interfaces, packages | class, interface, enum, extends, implements, package, import |
| Access modifiers | public, protected, private |
| Other modifiers | static, final, abstract, synchronized, volatile, transient, native, strictfp |
| Conditions | if, else, switch, case, default |
| Loops and jumps | for, while, do, break, continue, return |
| Exceptions | try, catch, finally, throw, throws, assert |
| Objects and references | new, this, super, instanceof |
| Reserved but unused | goto, const |
| Unnamed variable (Java 9+ keyword) | _ |
4. What Each Keyword Means
| Keyword | Meaning |
|---|---|
abstract | Class that cannot be instantiated, or method without a body. |
assert | Checks a condition during testing (enabled with -ea). |
break | Exits a loop or switch. |
case / default | Branches in a switch; default also marks interface default methods. |
catch / finally / try | Handle exceptions; finally always runs. |
class | Declares a class. |
continue | Skips to the next loop iteration. |
do / while / for | Loops. |
enum | Declares a fixed set of constants. |
extends / implements | Inherit from a class / implement an interface. |
final | Cannot be changed, overridden or extended. |
if / else | Conditional branches. |
import / package | Use classes from other packages / declare the current package. |
instanceof | Tests an object's type (with pattern matching since Java 16). |
interface | Declares an interface. |
native | Method implemented in another language (C/C++). |
new | Creates an object. |
return | Ends a method, optionally returning a value. |
static | Belongs to the class rather than to objects. |
strictfp | Strict floating-point rules (has no effect since Java 17, where all arithmetic is strict). |
super / this | Refer to the parent class / the current object. |
switch | Multi-way branch statement or expression. |
synchronized | Allows only one thread at a time in a method or block. |
throw / throws | Throw an exception / declare that a method may throw one. |
transient | Field skipped during serialization. |
void | Method returns nothing. |
volatile | Field always read from and written to main memory, visible to all threads. |
Detailed tutorials: this, super, final, static, transient, volatile, native and strictfp.
5. Contextual Keywords
Newer Java features added words that are keywords only in specific positions. That way, old programs that used these words as names still compile.
| Contextual keyword | Since | Used for |
|---|---|---|
var | Java 10 | Local variable type inference. |
yield | Java 14 | Return a value from a switch expression block. |
record | Java 16 | Declare a record class. |
sealed, non-sealed, permits | Java 17 | Restrict which classes may extend a class or interface. |
when | Java 21 | Guard condition in a pattern case. |
module, open, requires, exports, opens, uses, provides, to, with, transitive | Java 9 | Module declarations in module-info.java. |
Because they are contextual, this surprising code is legal:
public class ContextualKeywords {
public static void main(String[] args) {
// Contextual keywords are only special in certain places,
// so they can still be used as ordinary names elsewhere:
int record = 5;
String yield = "yield as a variable name";
var var = record * 2; // 'var' used as a type AND as a name
System.out.println(record + ", " + var + ", " + yield);
// ...and as keywords where they have meaning:
String size = switch (var) {
case 10 -> "ten";
default -> {
yield "other"; // 'yield' returns a value from a switch block
}
};
System.out.println("size = " + size);
}
}
5, 10, yield as a variable name size = ten
Legal does not mean recommended: avoid naming variables after contextual keywords in new code.
6. Reserved Literals: true, false and null
true and false are the two boolean values, and null means "no object". They are not technically keywords, but they are reserved, so you cannot use them as names either.
7. goto and const
goto and const are reserved but do nothing. Java's designers kept them out of the language (labeled break/continue replace goto, and final replaces const) while still reserving them, so that C and C++ programmers get a clear error instead of confusing behaviour.
8. Using a Keyword as a Name
Trying to name a variable class or using goto gives compile errors such as:
Bad.java:2: error: <identifier> expected
int class = 1;
^
Bad.java:3: error: illegal start of expression
void m() { goto end; }
Choose a different name, such as className or clazz (a common convention).
9. Tips for Remembering Keywords
- All keywords are lowercase;
String,SystemandIntegerare class names, not keywords. mainis not a keyword; it is just the name of the entry-point method.- IDEs highlight keywords in a different colour, which helps spot mistakes.
- Learn keywords together with the feature they belong to (exceptions, loops, OOP) rather than as a list.
10. Summary
- Keywords are reserved words with fixed meanings; they cannot be used as identifiers.
- Java has 51 reserved keywords, including the unused
gotoandconstand the underscore_. true,falseandnullare reserved literals.- Contextual keywords such as
var,record,yield,sealedandwhenare special only in certain positions.
Related Tutorials
Reference: Java Language Specification §3.9: Keywords
Frequently Asked Questions (FAQ)
Q1. What are keywords in Java?
Reserved words with predefined meanings in the language, such as class, public, if and return. They cannot be used as names for variables, methods or classes.
Q2. How many keywords are there in Java?
Java 21 defines 51 reserved keywords (including _, goto and const) plus 16 contextual keywords. The literals true, false and null are reserved too but are not counted as keywords.
Q3. Are true, false and null keywords?
Technically they are literals, not keywords, but they are reserved and cannot be used as identifiers.
Q4. Is main a keyword in Java?
No. main is an ordinary method name that the JVM looks for. You could even name a variable main.
Q5. Is String a keyword?
No. String is a class in java.lang. Keywords are always lowercase.
Q6. Why are goto and const reserved if they are not used?
To prevent their use as identifiers and to give clear errors to programmers coming from C and C++. Java offers labeled break/continue and final instead.
Q7. Is var a keyword in Java?
var is a reserved type name (a contextual keyword). It infers the type of local variables, but it can still be used as a variable or method name, though not as a class name.
Q8. What are contextual keywords?
Words that act as keywords only in certain places, such as record, yield, sealed, permits, when and the module keywords. Elsewhere they are normal identifiers, which keeps old code compiling.
Q9. Can I use a keyword as a variable name?
No, not a reserved keyword. The compiler reports errors such as <identifier> expected. Use a different name, for example clazz instead of class.
Q10. Are Java keywords case-sensitive?
Yes. Keywords must be lowercase. Class or IF are not keywords and could technically be identifiers, though using them is confusing.
Q11. What is the difference between throw and throws?
throw actually throws an exception object inside a method. throws appears in a method declaration to say which exceptions it may throw. See throw vs throws.
Q12. What does the underscore keyword do?
Since Java 9, a single _ cannot be an identifier. Since Java 22 it marks unnamed variables and patterns, for example catch (Exception _).
Q13. What does the yield keyword do?
Inside a switch expression, yield returns a value from a case block written with braces, for example default -> { yield "other"; }.
Q14. Is strictfp still useful?
No. Since Java 17, all floating-point arithmetic is strict by default, so strictfp has no effect and only produces a warning.
Q15. What is the difference between final, finally and finalize?
final is a keyword that prevents change; finally is a keyword for a block that always runs after try; finalize() is a deprecated method of Object, not a keyword. See final vs finally vs finalize.
Q16. Which keywords are used for exception handling?
try, catch, finally, throw and throws. assert is related, for checking assumptions during testing.
Q17. Which keywords are access modifiers?
public, protected and private. Default (package-private) access has no keyword.
Q18. Which keywords relate to multithreading?
synchronized (mutual exclusion) and volatile (visibility of field updates between threads).
Q19. What is the instanceof keyword?
It checks whether an object is an instance of a type. Since Java 16 it can also bind a variable: if (obj instanceof String s) { ... s.length() ... }. See instanceof operator.
Q20. Do new Java versions add keywords?
Rarely as reserved keywords, because that would break old code. New features usually add contextual keywords instead, such as record (Java 16) and when (Java 21).