Java Notes
Complete reference of all 67 Java keywords and reserved words — categorized by purpose, with explanations, usage examples, and rules for each keyword group.
Keywords are reserved words in Java that have predefined meanings and cannot be used as identifiers (variable names, class names, method names). Java has 67 reserved words (52 keywords + 2 reserved literals + 3 unused reserved words + contextual keywords).
Complete List of Java Keywords
// All 52 Java keywords organized by category:
// (This code won't compile — it's for reference only)
// Access Modifiers (4)
public, private, protected, default
// Class/Interface/Object (8)
class, interface, enum, record, extends, implements, new, instanceof
// Method/Variable Modifiers (8)
static, final, abstract, synchronized, volatile, transient, native, strictfp
// Control Flow (12)
if, else, switch, case, default, for, while, do, break, continue, return, yield
// Exception Handling (5)
try, catch, finally, throw, throws
// Type Keywords (10)
byte, short, int, long, float, double, char, boolean, void, var
// Package (2)
package, import
// Other (3)
this, super, assert
// Reserved Literals (2) — not technically keywords but reserved
true, false, null
// Unused Reserved Words (3) — reserved but have no function
goto, const, _Keywords by Category with Examples
Access Modifiers
public class AccessModifiers {
public String publicField = "Anyone can access";
private String privateField = "Only this class";
protected String protectedField = "This class + subclasses + same package";
String defaultField = "Same package only (no keyword)";
public static void main(String[] args) {
AccessModifiers obj = new AccessModifiers();
System.out.println(obj.publicField);
System.out.println(obj.privateField); // OK (same class)
}
}| Modifier | Class | Package | Subclass | World |
|---|---|---|---|---|
| public | ✅ | ✅ | ✅ | ✅ |
| protected | ✅ | ✅ | ✅ | ❌ |
| default | ✅ | ✅ | ❌ | ❌ |
| private | ✅ | ❌ | ❌ | ❌ |
Class and Object Keywords
// class — defines a class
public class Animal {
String name;
// this — refers to current object
public Animal(String name) {
this.name = name;
}
}
// extends — inheritance
class Dog extends Animal {
// super — refers to parent class
public Dog(String name) {
super(name); // calls Animal constructor
}
}
// interface — defines a contract
interface Swimmable {
void swim();
}
// implements — fulfills an interface
class Duck extends Animal implements Swimmable {
public Duck(String name) { super(name); }
public void swim() {
System.out.println(name + " is swimming");
}
}
// enum — defines fixed constants
enum Direction { NORTH, SOUTH, EAST, WEST }
// new — creates objects
// instanceof — checks type
class KeywordDemo {
public static void main(String[] args) {
Animal animal = new Dog("Rex"); // new keyword
if (animal instanceof Dog) { // instanceof keyword
System.out.println("It's a dog!");
}
Direction dir = Direction.NORTH;
System.out.println("Direction: " + dir);
}
}It's a dog! Direction: NORTH
Modifier Keywords
Count: 2 PI: 3.14159
Control Flow Keywords
Good Monday is a Weekday Even numbers: 2 4 6 8 Max: 20
Exception Handling Keywords
public class ExceptionKeywords {
public static void main(String[] args) {
// try, catch, finally
try {
int result = divide(10, 0);
} catch (ArithmeticException e) {
System.out.println("Caught: " + e.getMessage());
} finally {
System.out.println("Finally always executes");
}
// throws — declares possible exceptions
try {
riskyMethod();
} catch (Exception e) {
System.out.println("Handled: " + e.getMessage());
}
}
static int divide(int a, int b) {
return a / b; // may throw ArithmeticException
}
// throw — explicitly throws an exception
// throws — declares that this method might throw
static void riskyMethod() throws Exception {
throw new Exception("Something went wrong!");
}
}Caught: / by zero Finally always executes Handled: Something went wrong!
Type Keywords
public class TypeKeywords {
public static void main(String[] args) {
// All 8 primitive type keywords
byte b = 127;
short s = 32000;
int i = 2_000_000_000;
long l = 9_000_000_000L;
float f = 3.14f;
double d = 3.14159265358979;
char c = 'A';
boolean bool = true;
// void — method returns nothing
printInfo(b, s, i, l, f, d, c, bool);
// var (Java 10+) — local type inference (contextual keyword)
var message = "Hello"; // inferred as String
var numbers = java.util.List.of(1, 2, 3);
System.out.println("var message: " + message);
}
static void printInfo(byte b, short s, int i, long l,
float f, double d, char c, boolean bool) {
System.out.printf("byte=%d, short=%d, int=%d, long=%d%n", b, s, i, l);
System.out.printf("float=%.2f, double=%.4f, char=%c, boolean=%b%n", f, d, c, bool);
}
}byte=127, short=32000, int=2000000000, long=9000000000 float=3.14, double=3.1416, char=A, boolean=true var message: Hello
Contextual Keywords (Java 9+)
These are identifiers that act as keywords only in specific contexts:
| Keyword | Context | Introduced |
|---|---|---|
var | Local variable declarations | Java 10 |
yield | Switch expressions | Java 14 |
record | Record class declarations | Java 16 |
sealed, permits | Sealed class declarations | Java 17 |
non-sealed | Unsealing a class | Java 17 |
// These can still be used as variable names (backward compatibility):
int var = 10; // valid! (var is contextual)
int yield = 5; // valid!
String record = "test"; // valid!Keywords You Cannot Use as Identifiers
public class KeywordRestrictions {
public static void main(String[] args) {
// INVALID — these are keywords:
// int class = 5; // ERROR
// String for = "x"; // ERROR
// boolean true = false; // ERROR
// int null = 0; // ERROR
// VALID — similar but not keywords:
int Class = 5; // uppercase C (but bad practice)
String For = "x"; // uppercase F (but bad practice)
int _class = 10; // with underscore
int klass = 10; // alternative spelling
// goto and const are reserved but unused:
// goto label; // ERROR: not implemented
// const int X = 5; // ERROR: use 'final' instead
System.out.println("Keywords cannot be identifiers!");
}
}Common Mistakes
- Using keywords as variable names —
int class = 5;orString new = "x";won't compile. Keywords are permanently reserved. - Confusing
defaultkeyword usage —defaultappears in switch statements, interface methods (Java 8+), and annotations. Context determines its meaning. - Thinking
true,false,nullare keywords — technically they're "reserved literals", but the practical effect is the same: you can't use them as identifiers. - Forgetting that
gotois reserved — Java reservedgotobut never implemented it. You cannot use it as a variable name OR as a statement. - Using
varthinking it makes Java dynamic —varis compile-time type inference, not dynamic typing. The type is fixed once inferred.
Interview Questions
Q1: How many keywords does Java have?
Answer: Java has 52 keywords plus 2 reserved literals (true, false, null) and 3 unused reserved words (goto, const, _). Additionally, Java 9+ introduced contextual keywords (var, yield, record, sealed, permits) that are keywords only in specific contexts.
Q2: What is the difference between final, finally, and finalize?
Answer: final is a keyword/modifier: makes variables constant, methods non-overridable, and classes non-inheritable. finally is a block in try-catch that always executes regardless of exceptions. finalize() was a method called by GC before destroying an object (deprecated in Java 9, removed in Java 18).
Q3: Can you use goto in Java?
Answer: No. goto is a reserved keyword that has no implementation in Java. It was reserved to prevent its use as an identifier and to leave the possibility of future implementation (which never happened). Java uses structured control flow (loops, break with labels) instead.
Q4: What are contextual keywords?
Answer: Contextual keywords are identifiers that act as keywords only in specific syntactic positions. Examples: var (in local variable declarations), yield (in switch expressions), record (in record declarations), sealed/permits (in sealed classes). They can still be used as variable/method names for backward compatibility.
Q5: What is the difference between static and final?
Answer: static means the member belongs to the class rather than an instance (shared across all objects). final means the value/implementation cannot be changed after initialization. They serve different purposes and can be combined: static final creates a class-level constant.
Summary
Java keywords are reserved identifiers with predefined meanings that form the language's syntax. They control access (public, private), define structure (class, interface), manage flow (if, for, return), handle errors (try, catch, throw), and specify types (int, boolean). Memorizing all keywords isn't necessary, but understanding the major categories and their roles is essential for writing and reading Java code correctly.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Keywords in Java.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java Master Course topic.
Search Terms
java-master-course, java master course, java, master, course, fundamentals, syntax, basics
Related Java Master Course Topics