Java keywords are reserved words that have predefined meanings in the Java programming language. These cannot be used as identifiers (variable names, method names, class names, etc.) because they serve specific purposes in the language’s syntax and structure.
List of Java Keywords
Here is a complete list of Java’s reserved keywords:
Category | Keywords |
---|---|
Control Flow | if , else , switch , case , default , do , while , for , break , continue , return , goto |
Access Modifiers | private , protected , public |
Class Modifiers | class , abstract , final , interface , enum |
Object Handling | new , this , super , instanceof |
Exception Handling | try , catch , finally , throw , throws |
Data Types | byte , short , int , long , float , double , char , boolean |
Others | static , void , volatile , synchronized , native , transient , strictfp , assert , import , package |
Reserved for Future | const , null , true , false (Though null , true , and false are literals, they are reserved words). |
Categories of Keywords
1. Control Flow Keywords
These are used to control the flow of execution in a program.
-
if
,else
: Used for conditional branching.if (x > 0) { System.out.println("Positive"); } else { System.out.println("Negative"); }
-
switch
,case
,default
: Used for multi-way branching.switch (day) { case 1: System.out.println("Monday"); break; default: System.out.println("Invalid day"); }
-
break
,continue
: Used to exit or skip loops. -
return
: Exits a method and optionally returns a value.
2. Access Modifiers
Define the visibility of classes, methods, and variables.
public
: Accessible from anywhere.protected
: Accessible within the same package or subclasses.private
: Accessible only within the same class.
3. Class and Object Handling
class
,abstract
,interface
: Used to define classes or interfaces.this
: Refers to the current object.super
: Refers to the parent class.
4. Exception Handling
These keywords handle runtime errors in programs.
try
,catch
,finally
: Used to handle exceptions.try { int result = 10 / 0; } catch (ArithmeticException e) { System.out.println("Cannot divide by zero."); } finally { System.out.println("End of program."); }
5. Primitive Data Types
Define variables with specific data types.
int
,float
,double
: Numeric types.char
: Character type.boolean
: True or false values.
Special Keywords
1. static
Defines a class-level member shared across all objects.
class Example { static int count = 0; }
2. final
Defines constants or prevents inheritance/overriding.
final int MAX = 100; // Value cannot change
3. volatile
Indicates that a variable’s value can be changed by multiple threads.
Reserved Keywords
Java reserves certain keywords for future use:
goto
: Reserved but not implemented.const
: Usefinal
instead.
Key Points
- Keywords cannot be used as identifiers.
- Java is case-sensitive:
int
is a keyword, butInt
is not. - Reserved keywords like
goto
andconst
are not currently in use but cannot be used as identifiers.