Java Keywords Reference
A keyword is a word that has a special, fixed meaning in the Java language. Keywords are reserved by the compiler, so you cannot use them as names for variables, methods, or classes.
What Makes a Word a Keyword?
Every programming language reserves certain words for its own syntax. In Java, words like class, if, and return are part of the grammar the compiler understands. If you try to name a variable class or int, the code will not compile, because the compiler expects those words to appear only in their special roles.
Keywords vs. Identifiers
An identifier is a name you choose, such as total, userName, or calculateArea. A keyword is a name Java has already chosen. You are free to pick almost any identifier you like, as long as it is not one of the reserved keywords below.
The Full List of Java Keywords
| Keyword | Purpose |
|---|---|
abstract |
Declares a class or method that must be implemented by a subclass |
assert |
Tests an assumption during debugging |
boolean |
Declares a variable that holds true or false |
break |
Exits a loop or switch statement early |
byte |
Declares an 8-bit integer variable |
case |
Defines a branch inside a switch statement |
catch |
Handles an exception thrown by a try block |
char |
Declares a variable that holds a single character |
class |
Defines a new class |
const |
Reserved for future use; not actually usable in Java |
continue |
Skips to the next iteration of a loop |
default |
Defines the fallback branch of a switch, or a default interface method |
do |
Starts a do-while loop |
double |
Declares a double-precision floating-point variable |
else |
Defines the alternate branch of an if statement |
enum |
Defines a fixed set of named constants |
extends |
Declares that a class inherits from a superclass |
final |
Marks a variable, method, or class as unchangeable or non-overridable |
finally |
Defines code that always runs after a try block |
float |
Declares a single-precision floating-point variable |
for |
Starts a for loop |
goto |
Reserved for future use; not actually usable in Java |
if |
Starts a conditional branch |
implements |
Declares that a class implements an interface |
import |
Brings another package’s classes into scope |
instanceof |
Tests whether an object is an instance of a given type |
int |
Declares a 32-bit integer variable |
interface |
Defines a contract of methods a class can implement |
long |
Declares a 64-bit integer variable |
native |
Marks a method implemented outside Java, in platform-specific code |
new |
Creates a new object |
package |
Declares the package a file belongs to |
private |
Restricts access to within the same class |
protected |
Restricts access to the same package and subclasses |
public |
Allows access from any other class |
return |
Exits a method and optionally sends back a value |
short |
Declares a 16-bit integer variable |
static |
Ties a member to the class itself rather than to an instance |
strictfp |
Forces strict floating-point calculations |
super |
Refers to the parent class |
switch |
Starts a multi-branch conditional statement |
synchronized |
Marks code that only one thread may run at a time |
this |
Refers to the current object |
throw |
Raises an exception |
throws |
Declares that a method might raise an exception |
transient |
Excludes a field from serialization |
try |
Starts a block of code that may raise an exception |
void |
Declares that a method returns no value |
volatile |
Marks a field whose value may change unexpectedly across threads |
while |
Starts a while loop |
const and goto: Reserved but Unused
const and goto appear in the keyword list, but Java does not actually implement them. They are reserved simply so that no one can use them as identifiers, avoiding confusion for programmers coming from C or C++.
Contextual Keywords
A few words act like keywords only in specific positions, and can still be used as identifiers elsewhere. These include var (local variable type inference), yield (returning a value from a switch expression), record, sealed, and permits. They are sometimes called restricted keywords.
true, false, and null
true, false, and null are not keywords technically, they are reserved literals. In practice they behave the same way: you cannot use them as identifiers.
Example: Several Keywords Together
Here is a short program that uses public, class, static, void, final, int, boolean, for, if, else, and break in one place.
public class Main {
public static void main(String[] args) {
final int limit = 5;
boolean found = false;
for (int i = 0; i < limit; i++) {
if (i == 3) {
found = true;
break;
}
}
if (found) {
System.out.println("Found the value 3 before reaching the limit.");
} else {
System.out.println("Value not found.");
}
}
}
Output:
Found the value 3 before reaching the limit.
Keywords are the vocabulary of Java’s grammar. You have already been using many of them throughout this course, this reference is here whenever you need to check what one of them means.
