Reserved Words in Java
Introduction
In Java, some words are already reserved to perform specific tasks.
These words have special meaning in the Java language and cannot be used as identifiers such as variable names, method names, or class names.
Such predefined words are called reserved words in Java.
What Are Reserved Words in Java?
Reserved words are predefined keywords that are used by the Java compiler to understand the structure and logic of a program.
👉 If we try to use any reserved word as an identifier, the compiler will generate a compile-time error.
Categories of Reserved Words in Java
Java reserved words are grouped based on their purpose.
Reserved Words for Data Types
These keywords are used to define primitive data types.
byteshortintlongfloatdoublecharboolean
Reserved Words for Flow Control
These keywords control the execution flow of a program.
ifelseswitchcasedefaultfordowhilebreakcontinuereturn
Keywords Used as Modifiers
These keywords define access level and behavior of variables, methods, or classes.
publicprivateprotectedstaticfinalabstractsynchronizednativestrictfp(introduced in Java 1.2)transientvolatile
Keywords for Exception Handling
These keywords help handle runtime errors.
trycatchfinallythrowthrowsassert(introduced in Java 1.4)
Class-Related Keywords
These keywords are used while working with classes and packages.
classpackageimportextendsimplementsinterface
Object-Related Keywords
These keywords are related to object creation and reference.
newinstanceofsuperthis
Void Return Type Keyword
If a method does not return any value, it must use the void keyword in Java.
void
👉 In Java, using void is mandatory, unlike C++ where it can be optional.
Unused Keywords in Java
Some keywords are reserved but not used in Java.
goto
👉 Banned in Java because it creates complex and confusing code.const
👉 Java usesfinalinstead ofconst.
⚠️ Using these keywords in a program will cause a compile-time error.
Reserved Literals in Java
These are fixed values and not keywords.
true– value for boolean typefalse– value for boolean typenull– default value for object references
Enum Keyword
The enum keyword was introduced in Java 1.5.
It is used to define a group of named constants.
Example
enum Beer {
KF, RC, KO, FO;
}
Important Notes
All Java reserved words are written in lowercase
Java is case-sensitive
String,Integer, andmainare not keywordsfinalize()is a method, not a keyword
Which of the following are valid Java keywords?
| Word | Valid |
|---|---|
| public | ✅ |
| static | ✅ |
| void | ✅ |
| main | ❌ |
| String | ❌ |
| args | ❌ |
Conclusion
Reserved words are the core building blocks of the Java language. They help the compiler understand how the program should behave. Since these words have predefined meanings, they cannot be used as identifiers. Learning reserved words is essential for writing error-free Java programs.
Keep learning Java with EasyHaiCode 🚀