Java Keywords List and Basic Java Syntax
Java keywords are reserved words with a special meaning, like class,
if, and return. You can't use them as names for your own variables or classes. This page
lists all Java keywords with a plain-English meaning for each, plus the basic syntax rules every
Java program follows.
What are keywords in Java?
A keyword is a word the Java compiler already understands. When it sees while, it knows a loop is
starting. Because these words are taken, int while = 3; won't compile. Every Java keyword is
written in lowercase.
How many keywords does Java have?
Java has 51 reserved keywords (counting const, goto, and
_, which are reserved but unused), plus 17 contextual keywords such as
var and record that only act as keywords in certain places.
Types of keywords in Java
It's much easier to remember keywords in groups. Pick a group to see its members:
Java keywords by type
Data types
Declare what kind of value a variable holds.
booleanbytecharshortintlongfloatdoublevoid— a method returns nothing
Control flow
Decide which code runs, and how often.
ifelseswitchcasedefaultforwhiledobreakcontinuereturn
Exceptions
Signal and handle errors.
trycatchfinallythrowthrows— see throw vs throwsassert— check an assumption during testing
Classes & objects
Define types and create and refer to objects.
classinterfaceenumextendsimplementsnewthissuperinstanceofpackageimport
Modifiers
Change visibility or behaviour of classes, methods, and fields.
publicprotectedprivate— access modifiersstaticfinalabstractsynchronizedvolatiletransientnativestrictfp
Reserved, unused
Reserved so they can never be used as names, even though they do nothing.
constgoto_(underscore) — reserved since Java 9; since Java 22 it marks an unused variable
Complete list of all Java keywords
All 51 reserved keywords, in alphabetical order. Links go to our in-depth guides:
| Keyword | What it does |
|---|---|
abstract | Class can't be instantiated, or method has no body |
assert | Checks a condition while testing (enabled with -ea) |
boolean | true/false type |
break | Exits a loop or switch immediately |
byte | 8-bit whole number (−128 to 127) |
case | One branch of a switch |
catch | Handles an exception thrown in a try block |
char | A single 16-bit Unicode character |
class | Declares a class |
const | Reserved, unused (use final instead) |
continue | Skips to the next loop iteration |
default | Fallback switch branch; also interface default methods |
do | Starts a do-while loop (runs at least once) |
double | 64-bit decimal number |
else | Runs when the if condition is false |
enum | Declares a fixed set of named constants |
extends | Inherits from a class (or extends an interface) |
final | Can't be reassigned, overridden, or extended |
finally | Block that always runs after try/catch |
float | 32-bit decimal number |
for | Loop with a counter, or over a collection |
goto | Reserved, unused |
if | Runs code only when a condition is true |
implements | Class signs an interface's contract |
import | Uses classes from another package |
instanceof | Tests an object's type |
int | 32-bit whole number |
interface | Declares an interface |
long | 64-bit whole number |
native | Method implemented in another language (e.g. C) |
new | Creates an object or array |
package | Declares which package a file belongs to |
private | Visible only inside the class |
protected | Visible in the package and to subclasses |
public | Visible everywhere |
return | Exits a method, optionally with a value |
short | 16-bit whole number |
static | Belongs to the class, not to objects |
strictfp | Obsolete since Java 17 (all maths is strict now) |
super | Refers to the parent class |
switch | Chooses a branch based on a value |
synchronized | Lets only one thread run the code at a time |
this | Refers to the current object |
throw | Throws an exception now |
throws | Declares exceptions a method may throw |
transient | Field is skipped during serialization |
try | Starts a block whose exceptions can be caught |
void | Method returns nothing |
volatile | Field changes are immediately visible to all threads |
while | Loop that repeats while a condition is true |
_ | Reserved; unnamed variable since Java 22 |
Not keywords, but still off-limits: true, false, and null are literals. You can't use them as names either.
Contextual keywords in modern Java
New Java versions needed new keywords without breaking old code that might use those words as variable names. The solution: contextual keywords, which only have special meaning in a specific position.
| Contextual keyword | Meaning |
|---|---|
var | Local variable with inferred type (Java 10) |
record | Compact immutable data class (Java 16) |
sealed / non-sealed / permits | Restrict which classes may extend a type (Java 17) |
yield | Returns a value from a switch expression (Java 14) |
when | Adds a condition to a switch pattern case (Java 21) |
module, requires, exports, opens, open, uses, provides, to, with, transitive | Only inside module-info.java (Java 9) |
var names = new java.util.ArrayList<String>(); // var: type inferred as ArrayList<String>
record Point(int x, int y) { } // record: fields, constructor, equals, toString for free
String size = switch (names.size()) { // switch expression
case 0 -> "empty";
case 1, 2, 3 -> "small";
default -> {
yield "large"; // yield: value from a block
}
}; Basic Java syntax rules
Beyond keywords, every Java file follows a handful of rules. Here's a small program that shows them all:
package com.example.shop; // 1. optional package line comes first
import java.util.List; // 2. imports next
/** 3. Javadoc comment describing the class */
public class PriceList { // 4. public class name == file name (PriceList.java)
private static final int MAX_ITEMS = 100; // 5. constants in UPPER_SNAKE_CASE
public static void main(String[] args) {
List<Double> prices = List.of(4.5, 10.0, 2.25);
double total = 0; // 6. statements end with ;
for (double price : prices) { // 7. blocks use { }
total += price; // single-line comment
}
System.out.println("Total: " + total);
}
} Total: 16.75
| Rule | Example |
|---|---|
| Java is case-sensitive | total and Total are different variables |
| Statements end with a semicolon | int count = 0; |
| Code blocks are wrapped in braces | if (x > 0) { ... } |
Names may use letters, digits, _, $, but can't start with a digit | item2 ✔, 2item ✘ |
| Classes use PascalCase | PriceList, BankAccount |
| Methods and variables use camelCase | getTotal(), itemCount |
| Comments | // line, /* block */, /** Javadoc */ |
Quick recap
- Java has 51 reserved keywords; all are lowercase and can't be used as names.
constandgotoare reserved but unused;true,false, andnullare literals.- Contextual keywords like
var,record,sealed, andyieldonly apply in specific positions. - Java is case-sensitive, uses
;and{ }, and follows PascalCase/camelCase naming.
What's next?
Dive into the keywords beginners find trickiest: this,
super, static,
and final.
Frequently Asked Questions
How many keywords are there in Java?
Java has 51 reserved keywords, including const, goto, and the underscore (_), which are reserved but not used for their original meaning. Modern Java also has 17 contextual keywords, such as var, record, sealed, and yield, which only act as keywords in specific places.
Are true, false, and null keywords in Java?
Technically no. They're literals, which are fixed values built into the language. But they're reserved in the same way: you can't use true, false, or null as a variable or class name.
Can I use a Java keyword as a variable name?
Not a reserved keyword. 'int class = 5;' won't compile. Contextual keywords like var, record, or yield are allowed as variable names in most places for backward compatibility, but it's clearer to avoid them.
Why are goto and const reserved if Java doesn't use them?
Java's designers reserved them so programmers coming from C and C++ would get a clear compile error instead of confusing behaviour. Java uses labelled break and continue instead of goto, and final instead of const.
Is Java case-sensitive?
Yes. Every keyword is lowercase, and 'Class', 'CLASS', and 'class' are three different words to Java. Only 'class' is the keyword. Variable and method names are case-sensitive too, so 'total' and 'Total' are different variables.