Java Keywords Reference
›Short versionOpen if you only have a minute.
- A keyword is a word the compiler already owns. It cannot be a variable, method, or type name.
- Keywords are lowercase. String and System are class names, not keywords.
- No access modifier means package-private. It does not mean public.
- final stops reassignment or overriding. It does not freeze the object a reference points at.
- true, false, and null are reserved literals. var is special only in a local-variable position.
Keywords are tokens the compiler already understands. They control type declarations, flow, access, inheritance, exceptions, packages, modules, and concurrency, so they cannot be reused as ordinary identifiers.
Methods
| API | Parameters | Returns | Behavior | Watch for |
|---|---|---|---|---|
| class / interface / enum / record | Type name, members, optional inheritance or implementation clauses. | A compiled type declaration, not a runtime value. | Defines a new reference type that other code can instantiate, implement, or reference. | record is modern Java; older source levels will not compile it. |
| public / protected / private | Placed before classes, fields, constructors, or methods. | Access rules enforced at compile time. | Controls which packages, subclasses, and classes can access a member. | No modifier means package-private, not public. |
| static / final / abstract | Modifiers on fields, methods, classes, or nested declarations. | Changed declaration semantics. | static belongs to the class, final prevents reassignment or overriding, abstract requires implementation elsewhere. | final on an object reference does not make the object immutable. |
| if / switch / for / while | Conditions, cases, initializers, update expressions, or iterable values. | Control-flow decisions, not values in classic statements. | Directs which statements execute and how often loops repeat. | switch fall-through happens unless you break or use arrow-style cases. |
| try / catch / finally / throw / throws | Risky block, exception handlers, cleanup block, exception object, or method declaration. | Handled control flow or propagated failure. | Models exceptional paths separately from normal return values. | Catch specific exceptions before broader parent types. |
Which call fits
| Option | Best for | Tradeoff |
|---|---|---|
| Keyword | Core grammar such as class, if, return, and try. | Always reserved; cannot be used as variable, method, or class names. |
| Reserved literal | Values such as true, false, and null. | Acts like a special token even though it is not a normal identifier. |
| Contextual keyword | Features such as var, yield, record, or sealed in newer Java. | Meaning can depend on Java version and source position. |
Examples
Keywords shape declarations and flow
java
public final class Score {
private final int value;
public Score(int value) {
this.value = value;
}
public boolean isPassing() {
return value >= 50;
}
}Avoid keyword identifiers
java
// int class = 5; // Compile error
int classCount = 5; // Valid identifierA field is written int count; inside a class, with no public, protected, or private. Who can read it?
Other types in the same package can. Types in other packages cannot, and a subclass in another package cannot either. That is package-private access. Leaving the modifier off is not the same as writing public.