By WebNest Studio Engineering Team
Java - Core Tutorial
Java Keywords
Keywords are reserved words in Java that have a predefined meaning to the compiler and cannot be used as identifiers (variable names, method names, or class names). Java currently defines around 50 reserved keywords, plus a few reserved literals and contextual words.
Why it matters
Keywords are the fixed vocabulary of the language. You do not need to recite the list, but you do need to recognise each one on sight, because a keyword always means the same thing wherever it appears — and the compiler error when you accidentally use one as a name is not always obvious.
Watch: The Invisible Rules That Power Java
A quick video on Java keywords — the reserved words with a fixed meaning that you cannot use as names.
Categories of Keywords
Grouping keywords by purpose makes them far easier to remember than treating them as one flat list.
- Data type keywords: byte, short, int, long, float, double, char, boolean, void.
- Access modifiers: public, private, protected.
- Class/interface related: class, interface, extends, implements, abstract, final, static, package, import, new, this, super, instanceof.
- Control flow: if, else, switch, case, default, for, while, do, break, continue, return.
- Exception handling: try, catch, finally, throw, throws.
- Other: synchronized, volatile, transient, native, strictfp, assert, enum, const (reserved but unused), goto (reserved but unused).
Reserved Literals and Contextual Keywords
true, false, and null are technically reserved literals, not keywords, but they behave the same way: you cannot use them as identifiers. Newer Java versions also introduced "contextual keywords" like var, record, sealed, and yield, which are only treated specially in certain positions — you can still use words like "record" as a variable name in older code, unlike true keywords.
Example
Keywords in action across a small class
public abstract class Vehicle {
protected final String name;
public Vehicle(String name) {
this.name = name;
}
public abstract void move();
}
class Car extends Vehicle {
public Car(String name) {
super(name);
}
@Override
public void move() {
if (this.name != null) {
System.out.println(this.name + " is moving");
}
}
}
(Compiles cleanly — demonstrates abstract, extends, final, protected, super, this, @Override together)
Common Mistakes
- Trying to name a variable "class", "new", or "static" — the compiler will reject any reserved keyword used as an identifier.
- Treating "true", "false", and "null" as ordinary identifiers instead of reserved literals.
- Assuming "var" always means "any type, checked at runtime" — var is compile-time type inference, not a dynamic type.
Exercise
This program fails to compile because three variables are named with reserved words. Rename them to sensible identifiers so that it compiles.
Expected output: Order 42 total 199.5 new=true
public class OrderSummary {
public static void main(String[] args) {
int case = 42;
double double = 199.5;
boolean new = true;
System.out.println("Order " + case + " total " + double + " new=" + new);
}
}
Show hints
case,doubleandneware all reserved keywords.- Pick names that describe the data, such as an order number, a total and a flag.
Show solution
public class OrderSummary {
public static void main(String[] args) {
int orderNumber = 42;
double total = 199.5;
boolean isNew = true;
System.out.println("Order " + orderNumber + " total " + total + " new=" + isNew);
}
}
Quick quiz
1. Which of these is NOT a Java keyword?
- A. goto
- B. main
- C. const
- D. strictfp
Show answer
B. main is just a method name the JVM looks for. goto and const are reserved even though the language does not use them.
2. What does var do in var count = 10;?
- A. Makes count dynamically typed
- B. Makes count a constant
- C. Lets the compiler infer the type as int at compile time
- D. Declares a global variable
Show answer
C. var is compile-time type inference for local variables. count is an int and cannot later hold a String.
3. How does Java classify true, false and null?
- A. As keywords
- B. As operators
- C. As identifiers
- D. As reserved literals
Show answer
D. They are literals, not keywords, but they are reserved and cannot be used as identifiers either.
Interview questions
Are goto and const used in Java?
Show answer
No. Both are reserved keywords, so you cannot use them as identifiers, but the language gives them no function. They were reserved so the compiler can give a clear error if someone coming from C or C++ tries to use them. Java uses final where C++ would use const, and labelled break and continue where goto might have been used.
Key Points to Remember
- Keywords are reserved by the compiler and can never be used as variable, method, or class names.
- Grouping keywords by purpose (types, modifiers, control flow, exceptions) makes them easier to learn.
- true, false, and null are reserved literals; var, record, and sealed are newer contextual keywords.
Go deeper
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
These examples need a JDK project with their libraries on your computer, so they can be copied but not run here. Codelab runs Python, FastAPI backends with SQLite, plain Java 17 and HTML/CSS/JavaScript.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering