In Java, keywords are reserved words that have a predefined meaning in the language. These words form the core building blocks of Java’s syntax and cannot be used as identifiers (such as variable names, class names, or method names).
✔️ Example :
int age;
In this statement, int
is a keyword that tells the compiler the variable age
is of integer type (32-bit signed two’s complement integer). We cannot use int
as a variable name, class name, or method name. Attempting to do so results in a compilation error.
✅ What are Keywords in Java?
A keyword is a word that is reserved by Java because it has a special function in the language. Java has a total of 53 keywords (as of Java SE 17), and they cannot be redefined by the user.
✅ Why Are Keywords Important?
- Keywords define the structure and behavior of a Java program.
- They instruct the compiler to perform specific actions.
- Prevent the use of meaningful reserved words for other purposes, ensuring consistency.
✅Classification of Java Keywords
Let’s classify some of the most important Java keywords by their functionality:
1️⃣ Access Modifiers
In Java, Access Modifiers are keywords used to control the visibility and accessibility of classes, methods, and variables. They define which parts of your program can access a particular class member (variable, method, or constructor).
public
,private
,protected
✔️Example:
public class MyClass {
private int data; // Private variable
public void display() { // Public method
System.out.println(data);
}
}
2️⃣ Class, Interface, Inheritance Related
class
,interface
,extends
,implements
Example:
public interface Animal {
void sound();
}
public class Dog implements Animal {
public void sound() {
System.out.println("Bark");
}
}
public class AnimalTester {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
}
}
3️⃣ Data Type Keywords
int
,boolean
,char
,double
,float
,long
,short
,byte
Example:
public class DataTypes {
public static void main(String[] args) {
int number = 100;
double price = 99.99;
boolean isAvailable = true;
char grade = 'A';
System.out.println("Number: " + number);
System.out.println("Price: " + price);
System.out.println("Available: " + isAvailable);
System.out.println("Grade: " + grade);
}
}
4️⃣ Control Flow Keywords
if
,else
,switch
,case
,default
,for
,while
,do
,break
,continue
,return
Example:
public class ControlFlowExample {
public static void main(String[] args) {
int number = 5;
if (number > 0) {
System.out.println("Positive Number");
} else {
System.out.println("Non-positive Number");
}
for (int i = 0; i < 3; i++) {
System.out.println("Loop iteration: " + i);
}
}
}
5️⃣ Exception Handling Keywords
try
,catch
,finally
,throw
,throws
Example:
public class ExceptionExample {
public static void main(String[] args) {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("Execution completed");
}
}
}
6️⃣ Object Creation and Memory Management
new
,this
,super
Example:
class Parent {
Parent() {
System.out.println("Parent constructor");
}
}
class Child extends Parent {
Child() {
super(); // Calls Parent constructor
System.out.println("Child constructor");
}
}
public class ObjectCreation {
public static void main(String[] args) {
Child child = new Child();
}
}
7️⃣ Concurrency Keywords
synchronized
,volatile
Example:
class Counter {
private int count = 0;
public synchronized void increment() {
count++;
}
public int getCount() {
return count;
}
}
8️⃣ Miscellaneous
static
,final
,abstract
,const
,native
,strictfp
,transient
Example of final
and static
:
public class ConstantsExample {
public static final double PI = 3.14159;
public static void main(String[] args) {
System.out.println("Value of PI: " + PI);
}
}
🚨 Keywords You Should Never Use as Identifiers
int class = 10; // ❌ Invalid — 'class' is a reserved keyword
✅Complete List of Java Keywords (for Reference)
There are 51 standard keywords in Java that cannot be used as identifiers.
Keyword | Description |
---|---|
abstract | Used with classes/methods. An abstract class cannot be instantiated directly. An abstract method must be implemented by a child class. |
assert | Enables testing of assumptions in the program. |
boolean | Represents true or false values. |
break | Terminates a loop (for , while , do-while ) or a switch block. |
byte | Stores integer values from -128 to 127. |
case | A block in a switch statement. |
catch | Handles exceptions after a try block. |
char | Stores a single character. |
class | Defines a class. |
const | Reserved keyword (use final instead). |
continue | Skips the current iteration in a loop. |
default | Specifies the default block in switch , or default methods in interfaces. |
do | Executes a block repeatedly while a condition is true. |
double | 64-bit floating-point number. |
else | Provides alternative in if-else . |
enum | Defines a type with a fixed set of constants. |
extends | Inheritance from a superclass. |
final | Prevents reassignment of variables, method overriding, or subclassing. |
finally | Code that executes after a try-catch , regardless of outcome. |
float | 32-bit floating-point number. |
for | Loop that executes a set of statements repeatedly. |
goto | Reserved but not used. |
if | Conditional branch. |
implements | Implements an interface. |
import | Imports classes, packages, or interfaces. |
instanceof | Checks object type at runtime. |
int | 32-bit integer. |
interface | Declares an interface. |
long | 64-bit integer. |
native | Declares platform-dependent methods. |
new | Creates new objects. |
package | Defines a package for organizing classes. |
private | Access limited to the current class. |
protected | Accessible in current package or subclass. |
public | Accessible from anywhere. |
return | Returns a value from a method. |
short | 16-bit integer. |
static | Belongs to the class, not an instance. |
strictfp | Enforces strict floating-point behavior. |
super | Refers to parent class object. |
switch | Multiple execution paths based on a variable. |
synchronized | Ensures thread safety. |
this | Refers to the current object. |
throw | Explicitly throws an exception. |
throws | Declares exceptions a method can throw. |
transient | Prevents serialization of variables. |
try | Wraps code expected to throw exceptions. |
void | Method returns no value. |
volatile | Variable is always read from main memory. |
while | Loops while condition is true. |
_ (Underscore) | Since Java 9, used to prevent underscores as unused identifiers. |
⚠️ Special Notes
- The keywords
const
andgoto
are reserved but not currently used in Java. - The literals
true
,false
, andnull
are not keywords but literals. Still, they cannot be used as identifiers. - Keywords such as
strictfp
,assert
, andenum
were added in later JDK versions:strictfp
→ JDK 1.2assert
→ JDK 1.4enum
→ JDK 1.5
- Newer Java features, such as sealed classes, records, and JPMS (Java Platform Module System), introduced contextual keywords.
▶️Contextual Keywords
These words act as keywords in certain contexts but are valid as identifiers otherwise.
Keyword | Description |
---|---|
exports | Used for exporting modules. |
module | Declares a module. |
non-sealed | Used with sealed classes. |
open | Declares an open module. |
opens | Exports a module for reflection. |
permits | Defines allowed subclasses for sealed classes. |
provides | Used in module definition to specify service providers. |
record | Defines a compact data class. |
requires | Specifies dependencies between modules. |
sealed | Restricts class extension. |
to | Used in module declarations. |
transitive | Requires transitive dependencies. |
uses | Specifies a service interface for the module system. |
var | Inferred local variable type. |
with | Used in module declarations. |
yield | Returns a value from a switch expression. |
✔️Example of Contextual Keyword record
public record Person(String name, int age) {}
public class RecordExample {
public static void main(String[] args) {
Person person = new Person("Alice", 30);
System.out.println(person.name() + " - " + person.age());
}
}
🎯 Summary
Java keywords are the backbone of the language’s syntax. Understanding them is essential for writing clean, efficient, and error-free code. By mastering how and where to use them, developers can write maintainable and optimized Java applications.