Introduction
Keywords are reserved words that have predefined meanings in the Java language. They form the foundation of Java syntax and are used to define classes, methods, variables, loops, conditions, exception handling, packages, inheritance, interfaces, and many other language features.
Since Java keywords are reserved by the compiler, they cannot be used as variable names, class names, method names, or identifiers. Attempting to do so results in a compilation error.
What are Java Keywords?
Java keywords are reserved words that have special meanings predefined by the Java language. These words are recognized by the Java compiler and are used to define the structure and behavior of Java programs.
For example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
In the above program:
- public
- class
- static
- void
are all Java keywords.
Because these words already have predefined meanings, they cannot be used as identifiers.
Syntax
Keywords are used as part of Java statements.
Example:
public class Student {
}
Another example:
if (marks >= 40) {
System.out.println("Pass");
}
Here:
- public
- class
- if
are Java keywords.
List of Java Keywords
Modern Java provides approximately 50 reserved keywords (the exact set depends on the Java version, as newer releases have introduced contextual keywords such as record, sealed, permits, non-sealed, and yield).
Some of the most commonly used keywords are:
| Keyword | Purpose |
|---|---|
| abstract | Defines an abstract class or method |
| assert | Tests assumptions during debugging |
| boolean | Declares a boolean variable |
| break | Exits a loop or switch |
| char | Declares a character variable |
| case | Defines a case in a switch statement |
| catch | Handles exceptions |
| class | Declares a class |
| continue | Skips the current loop iteration |
| default | Specifies the default case in a switch |
| do | Starts a do-while loop |
| double | Declares a double variable |
| else | Defines an alternative condition |
| enum | Declares an enumeration |
| extends | Enables inheritance |
| final | Prevents modification or inheritance |
| finally | Executes after try-catch |
| float | Declares a float variable |
| for | Creates a for loop |
| if | Creates a conditional statement |
| implements | Implements an interface |
| import | Imports packages |
| instanceof | Checks an object’s type |
| int | Declares an integer variable |
| interface | Declares an interface |
| long | Declares a long variable |
| native | Indicates a native method |
| new | Creates an object |
| null | Represents the null reference literal (reserved literal) |
| package | Declares a package |
| private | Private access modifier |
| protected | Protected access modifier |
| public | Public access modifier |
| return | Returns a value from a method |
| short | Declares a short variable |
| static | Declares a class member |
| strictfp | Restricts floating-point calculations |
| super | Refers to the parent class |
| switch | Multi-way decision statement |
| synchronized | Thread synchronization |
| this | Refers to the current object |
| throw | Throws an exception |
| throws | Declares exceptions |
| transient | Excludes a field from serialization |
| try | Starts exception handling |
| void | Specifies no return value |
| volatile | Indicates a variable may be modified by multiple threads |
| while | Creates a while loop |
Note: true, false, and null are reserved literals rather than keywords, but they are also reserved and cannot be used as identifiers.
Categories of Java Keywords
Java keywords can be grouped based on their purpose.
1. Access Modifier Keywords
These keywords define the visibility of classes, methods, and variables.
- public
- private
- protected
Example:
public class Student {
private int age;
}
2. Data Type Keywords
These keywords declare primitive data types.
- int
- byte
- short
- long
- float
- double
- char
- boolean
Example:
int age = 25;
double salary = 55000.75;
3. Flow Control Keywords
These keywords control program execution.
- if
- else
- switch
- case
- default
- for
- while
- do
- break
- continue
- return
Example:
if (age >= 18) {
System.out.println("Eligible");
}
4. Object-Oriented Programming Keywords
These keywords support Java’s object-oriented features.
- class
- interface
- extends
- implements
- this
- super
- new
- abstract
- final
Example:
class Car {
}
5. Exception Handling Keywords
Used to detect and handle runtime errors.
- try
- catch
- finally
- throw
- throws
Example:
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error");
}
6. Multithreading Keywords
Used when working with concurrent programming.
- synchronized
- volatile
Example:
public synchronized void display() {
}
Example Program
public class KeywordsExample {
public static void main(String[] args) {
int marks = 80;
if (marks >= 40) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
}
}
Keywords Used
- public
- class
- static
- void
- int
- if
- else
Real-Life Example
Imagine you’re developing a Banking Application.
public class Account {
private double balance = 10000;
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
}
}
}
Keywords used:
- public
- class
- private
- double
- void
- if
These keywords help define the structure, visibility, and behavior of the application.
Why are Keywords Reserved?
Keywords are reserved because they have predefined meanings in Java.
For example:
class Student {
}
Here, class tells the compiler that you’re declaring a new class.
If developers were allowed to use class as a variable name, the compiler would not be able to distinguish between the keyword and an identifier.
Best Practices
When working with Java keywords:
- Never use keywords as variable or method names.
- Use meaningful identifiers instead.
- Learn the purpose of frequently used keywords first.
- Follow Java naming conventions.
- Keep your code readable and consistent.
- Use access modifiers appropriately.
- Understand the role of each keyword before using it.
- Stay updated with newer Java versions, which may introduce contextual keywords.
Common Mistakes
1. Using a Keyword as a Variable Name
Incorrect:
int class = 10;
Correct:
int classNumber = 10;
2. Using a Keyword as a Class Name
Incorrect:
class public {
}
Correct:
class Student {
}
3. Confusing == with equals()
Although not a keyword issue, beginners often confuse operators and methods.
Incorrect:
String a = new String("Java");
String b = new String("Java");
System.out.println(a == b);
Correct:
System.out.println(a.equals(b));
4. Misusing static
Not every variable or method should be declared static. Use it only when the member belongs to the class rather than individual objects.
5. Forgetting return in Non-void Methods
Incorrect:
public int add(int a, int b) {
}
Correct:
public int add(int a, int b) {
return a + b;
}
6. Using break Outside a Loop or switch
break can only be used inside loops or switch statements.
Conclusion
Java keywords are reserved words that define the syntax, structure, and behavior of Java programs. They are essential for declaring classes, variables, methods, loops, conditions, exception handling, inheritance, interfaces, multithreading, and many other language features.
Because keywords have predefined meanings, they cannot be used as identifiers.