How Many Keywords Are in Java?
Java has 51 reserved keywords as defined by the Java Language Specification. Two of them — goto and const — are reserved but not used by the language. Beyond the 51, several contextual keywords have been added in recent versions.
The 51 reserved keywords
| Col 1 | Col 2 | Col 3 | Col 4 | Col 5 |
|---|---|---|---|---|
| abstract | assert | boolean | break | byte |
| case | catch | char | class | const† |
| continue | default | do | double | else |
| enum | extends | final | finally | float |
| for | goto† | if | implements | import |
| instanceof | int | interface | long | native |
| new | package | private | protected | public |
| return | short | static | strictfp | super |
| switch | synchronized | this | throw | throws |
| transient | try | void | volatile | while |
| assert | enum |
† goto and const are reserved but cause a compile error if used.
How to count properly
Different sources give different counts (50, 51, 53) depending on whether they count:
gotoandconst(reserved but unused) — included in the JLS counttrue,false,null— reserved literals, not keywords per JLS- Contextual keywords like
record,sealed,var— not in the official reserved keyword count
The Java Language Specification §3.9 lists 51 keywords. If you include the three reserved literals, you get 54 words you cannot use as identifiers.
Contextual keywords (do not count in the 51)
var— local type inference (Java 10)record— record class declarations (Java 16)sealed,permits,non-sealed— sealed class/interface (Java 17)yield— switch expression value (Java 14)
These are "restricted identifiers" — you can still use them as variable or method names in contexts where the parser doesn't expect a type or class declaration, though doing so is confusing and inadvisable.
Example: why the count matters
Exam questions like "how many keywords does Java have?" expect 51. Pop-quiz questions asking "which of these is a keyword: String, int, main, class?" expect int and class — String is a class in the standard library and main is a conventional method name, neither is a keyword.