π§© Java Keywords β Complete Guide with List, Usage, and Examples (2025)
π§² Introduction β Why Learn Java Keywords?
Every programming language has its building blocks β in Java, those blocks are keywords. These are reserved words that the Java compiler understands and treats specially. You cannot use them as variable names, class names, or method identifiers.
Understanding Java keywords helps you:
- β Write syntactically correct Java code
- β Understand how Java works behind the scenes
- β Avoid naming conflicts and errors
By the end of this article, youβll know:
- π What Java keywords are
- β The full list of Java keywords (2025)
- π§ Practical usage examples
- π‘ Special notes for beginners and advanced developers
π What Are Java Keywords?
Java keywords are predefined, reserved words used to define the syntax and structure of Java programs. They are part of the Java language itself and cannot be used as identifiers (names for variables, methods, classes, etc.).
π Example of a Java keyword:
public class HelloWorld {
public static void main(String[] args) {
int number = 5;
}
}
β In this code:
public
,class
,static
,void
, andint
are Java keywords.- You cannot use
class
as a variable name likeint class = 10;
β it would result in a compile-time error.
π List of All Java Keywords (As of Java 17+)
Hereβs the complete list of Java keywords:
π Keyword | π Description |
---|---|
abstract | Declares a class or method as abstract |
assert | Used for debugging with assertions |
boolean | Declares a boolean data type |
break | Exits a loop or switch block |
byte | Declares a byte-type variable |
case | Defines a case in a switch statement |
catch | Handles exceptions in try-catch blocks |
char | Declares a character data type |
class | Declares a class |
const | Reserved but not used |
continue | Skips current loop iteration |
default | Default case in switch, or default method in interface |
do | Starts a do-while loop |
double | Declares a double-precision variable |
else | Executes block when if is false |
enum | Declares an enumerated type |
extends | Inheritance from a superclass |
final | Constant declaration or method/class that canβt be overridden |
finally | Executes after try-catch block |
float | Declares a floating-point variable |
for | Initiates a loop |
goto | Reserved, not used in Java |
if | Conditional statement |
implements | Implements an interface |
import | Imports Java packages |
instanceof | Checks object type |
int | Declares an integer variable |
interface | Declares an interface |
long | Declares a long-type variable |
native | Declares platform-dependent methods |
new | Allocates memory for objects |
null | Represents null reference |
package | Declares a package |
private | Access modifier β within class only |
protected | Access modifier β package and subclass |
public | Access modifier β global |
return | Exits method and optionally returns value |
short | Declares a short-type variable |
static | Denotes class-level member |
strictfp | Enables strict floating-point calculations |
super | Refers to parent class |
switch | Conditional branching |
synchronized | Thread-safety for blocks or methods |
this | Refers to current object |
throw | Throws an exception |
throws | Declares exceptions a method may throw |
transient | Prevents serialization of variable |
try | Starts exception-handling block |
void | Declares method with no return |
volatile | Declares a variable as volatile (multithreading) |
while | Starts a loop based on condition |
β Java Keyword Examples
π Example 1 β Using if
, else
, return
, int
:
public class NumberCheck {
public static String checkEvenOdd(int num) {
if (num % 2 == 0) {
return "Even";
} else {
return "Odd";
}
}
}
β Explanation:
public
,class
,int
,if
,else
,return
β all are keywords.- The method checks if a number is even or odd.
π Example 2 β Using for
, break
, continue
:
public class LoopControl {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 5) continue;
if (i == 8) break;
System.out.println(i);
}
}
}
β Explanation:
for
initializes the loop.continue
skips printing 5.break
stops loop at 8.
π Example 3 β Using try
, catch
, finally
, throw
:
public class ExceptionDemo {
public static void main(String[] args) {
try {
throw new Exception("Error occurred");
} catch (Exception e) {
System.out.println("Caught: " + e.getMessage());
} finally {
System.out.println("Finally block executed");
}
}
}
β Explanation:
try
block handles risky code.throw
generates an exception.catch
handles the exception.finally
executes cleanup code.
β οΈ Notes on const
and goto
const
andgoto
are reserved but not used in modern Java.- These are reserved for future compatibility but should be avoided in your code.
π‘ Tips for Using Java Keywords
- β Use meaningful variable names to avoid accidental use of keywords.
- π Avoid using words like
class
,new
,static
, etc., as identifiers. - β Stay updated with new Java versions β new keywords may be added.
π§ Summary β Why Keywords Matter
Java keywords are the foundation of writing valid Java code. Knowing them:
- β Prevents syntax errors
- β Boosts your understanding of the Java language
- β Makes your code cleaner and more readable
βFAQs on Java Keywords
β What are Java keywords used for?
Java keywords define the structure and rules of Java syntax. They instruct the compiler on how to interpret the code.
β Can I use Java keywords as variable names?
No. Java keywords are reserved and cannot be used as identifiers for variables, classes, or methods.
β Are true
, false
, and null
Java keywords?
These are literals, not technically keywords, but are reserved and cannot be used as identifiers.
β What happens if I accidentally use a keyword as a variable?
You’ll get a compile-time error. Example: int class = 5;
is invalid.
β How many Java keywords are there?
As of Java 17+, there are 50 official keywords and 2 reserved words (goto
, const
) not in use.
Share Now :