In Java, keywords are special words that are reserved by the compiler. These keywords serve predefined purposes and cannot be used as identifiers, such as variable names, class names, or method names.
Using a keyword inappropriately will result in a compile-time error.
🔍 What Are Java Keywords?
A keyword (also known as a reserved word) tells the Java compiler to treat it as part of the language syntax. You cannot redefine it, override it, or use it in custom identifiers.
Example:
int class = 10; // ❌ Invalid: 'class' is a keyword
📋 Complete List of Java Keywords
These 50 words are reserved by the Java language. They are case-sensitive and cannot be used for any other purpose.
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 | — | — |
* const
and goto
are reserved but not used in Java.
🛠️ Usage Rules and Best Practices
✅ Do This
int count = 10; // valid
String name = "Java"; // valid
❌ Avoid This
int for = 5; // Error: 'for' is a keyword
String new = "Data"; // Error: 'new' is a keyword
💡 Common Mistakes
Mistake | Fix |
---|---|
Using class , enum , or switch as identifiers |
Rename to something else |
Typing Class (uppercase) thinking it's different from class |
Java is case-sensitive, but keywords are always lowercase |
Using reserved keywords in libraries accidentally | Prefix with _ or use camelCase |
🎓 Interview Angle
-
Q: Why can’t I use ‘new’ or ‘for’ as variable names in Java?
A: Because they are reserved as part of the Java syntax. Using them would break parser logic and lead to ambiguity. -
Q: Are Java keywords case-sensitive?
A: Yes. All keywords are lowercase.Public
,Void
,Main
are not keywords.
🧭 Java Version Relevance
Keyword | Introduced | Notes |
---|---|---|
Most keywords | Java 1.0 | Included since the beginning |
enum |
Java 5 | Introduced for enumerated types |
assert |
Java 1.4 | Added for assertions |
strictfp |
Java 1.2 | Ensures consistent floating-point precision |
var |
Java 10 | Not a keyword but a reserved type name |
📝 Summary
- Java has 50 keywords, all reserved for internal use by the compiler.
- Using a keyword as an identifier will lead to errors.
- Two keywords (
const
andgoto
) are reserved but not currently in use. - All keywords are lowercase and cannot be redefined.
✅ Tip: When naming your variables or classes, always check against the list of Java keywords to avoid naming conflicts.