Java Keywords: Complete Guide to Java Reserved Words

Java-Keywords-Feature.jpg

If you’re a newcomer to Java, among the first things you should learn are the Java keywords. Java keywords are reserved words that have a predefined purpose in Java that the compiler recognizes. They are an integral part of the Java syntax and structure and cannot be used for variable, method, or class names. 

Java actually contains 50 total keywords, and two of them, goto and const, are recognized but not utilized, so you are left with a Java keywords list of 48 that you will use on a regular basis.

In this tutorial, we will examine what Java keywords are, arrange them into groups/categories, provide a Java keywords list that you can use as a cheat sheet, give some examples of how they work, and discuss how to use the keywords in your programs. Let’s get started.

Table of Contents:

What are Java Keywords?

In Java, keywords are reserved words that have meanings assigned to them by the compiler. Java keywords act like commands that stand for something in Java.

For example:

  •  if signals a conditional statement. 
  • class tells Java that the user is creating a blueprint for an object.

If the user were to use a keyword incorrectly and name a variable int or class, it would fail to compile.

Quick Java Keyword Example:

int int = 5; // This will cause a compile-time error
int number = 5; // Correct usage

Key Points About Java Keywords:

  • Keywords are case-sensitive (for example, if is valid, If is not).
  • They are reserved for specific purposes in Java.
  • They cannot be used as identifiers in your code.
  • IDEs highlight keywords differently to provide a way to distinguish keywords from ordinary variable names and method names.

Basically, knowing the Java keywords is imperative to writing error-free, performant, and easily readable code. Keywords are a building block for everything else you’ll learn within Java. Now, let’s explore the 50 Java keywords explained by categorizing them according to their purpose, along with some Java keywords examples.

1. Data Type Java Keywords

These Java keywords are used to declare variables of a specific data type.

  • byte: 8-bit integer
  • short: 16-bit integer
  • int: 32-bit integer
  • long: 64-bit integer
  • float: 32-bit floating-point
  • double: 64-bit floating-point
  • char: single 16-bit Unicode character
  • boolean: true or false

Java Data Type Keywords Example:

int age = 25;
boolean isJavaFun = true;
char grade = 'A';

2. Control Flow Java Keywords

These Java keywords control the flow of execution in a program.

  • if / else: conditional statements
  • switch / case / default: multi-way branching
  • for / while / do: loops
  • break / continue: manage loops

Java Control Flow Keywords Example:

for(int i = 1; i <= 5; i++) {
    if(i == 3) continue;
    System.out.println(i);
}

3. Java Access Modifiers and Non-Access Modifiers

These Java keywords control the access and behavior of classes, methods, and variables:

Access Modifiers:

  • public, private, protected: define accessibility

Non-Access Modifiers:

  • final: constant value, cannot be overridden
  • static: belongs to the class, not object
  • abstract: class or method without full implementation
  • synchronized: used in multi-threaded code
  • volatile: variable can be modified asynchronously
  • transient: fields not serialized
  • strictfp: ensures platform-independent floating-point calculations

Java Access Modifiers and Non-Access Modifiers Example:

public class MyClass {
    private static int count = 0;
    final float pi = 3.14f;
}

4. Java Object-Oriented Programming (OOP) Keywords

These Java keywords help implement core OOP concepts like inheritance, polymorphism, and abstraction:

  • class: declares a class
  • interface: declares an interface
  • extends: inheritance from a class
  • implements: implements an interface
  • super: refers to parent class
  • this: refers to current object

Java Object-Oriented Programming (OOP) Keywords Example:

class Parent {}
class Child extends Parent {}

5. Exception Handling Java Keywords

These Java keywords are used for handling errors or exceptions during program execution:

  • try: block to test for exceptions
  • catch: block to handle exceptions
  • finally: block always executed after try-catch
  • throw: explicitly throw an exception
  • throws: declare exceptions a method can throw

Java Exception Handling Keywords Example:

try {
    int result = 10 / 0;
} catch (ArithmeticException e) {
    System.out.println("Cannot divide by zero!");
} finally {
    System.out.println("Execution finished.");
}

By categorizing keywords, it becomes much easier to learn, remember, and use them effectively in your Java programs.

Java Keywords Cheatsheet

Here’s a quick reference for all the Java keywords explained along with their examples. This is perfect for beginners who want to learn fast or keep a handy guide while coding.

Keyword Category Purpose / Example
abstract OOP Declares an abstract class or method.abstract class Shape { abstract void draw(); }
assert Exception / Debugging Tests assumptions in code.assert x > 0;
boolean Data Type Declares boolean variables.boolean flag = true;
break Control Flow Exits a loop or switch case.if(i==5) break;
byte Data Type Declares 8-bit integer.byte b = 100;
case Control Flow Defines a case in switch.case 1: System.out.println(“One”); break;
catch Exception Handling Catches exceptions.catch(Exception e) {}
char Data Type Declares character variable.char c = ‘A’;
class OOP Declares a class.class MyClass {}
const Reserved Not used in Java.
continue Control Flow Skips current iteration.if(i==3) continue;
default Control Flow Default case in switch.default: System.out.println(“None”);
do Control Flow Executes loop at least once.do { … } while(condition);
double Data Type 64-bit floating-point.double d = 3.14;
else Control Flow Executes when if is false.if(x>0) … else …
enum OOP Declares enumerated type.enum Day { MON, TUE };
extends OOP Inherits class.class Child extends Parent {}
final Modifier Constant variable or method/class cannot be overridden.final int MAX = 10;
finally Exception Handling Always executed after try-catch.finally { … }
float Data Type 32-bit floating-point.float f = 4.5f;
for Control Flow Looping statement.for(int i=0;i<5;i++){}
if Control Flow Conditional statement.if(x>0) …
implements OOP Implements an interface.class MyClass implements MyInterface {}
import Package Imports classes or packages.import java.util.*;
instanceof OOP Checks object type.if(obj instanceof MyClass) …
int Data Type 32-bit integer.int num = 100;
interface OOP Declares an interface.interface Drawable {}
long Data Type 64-bit integer.long bigNum = 100000L;
native Modifier Method implemented in native code.public native void demo();
new OOP Creates object instance.MyClass obj = new MyClass();
null Literal Represents null reference.String str = null;
package Package Declares a package.package com.example;
private Access Modifier Accessible only within the class.private int count;
protected Access Modifier Accessible within package/subclass.protected int age;
public Access Modifier Accessible everywhere.public void display(){}
return Control Flow Returns from method.return value;
short Data Type 16-bit integer.short s = 1000;
static Modifier Belongs to class, not instance.static int count;
strictfp Modifier Ensures platform-independent FP calculations.strictfp class FPClass {}
super OOP Refers to parent class.super.method();
switch Control Flow Multi-branch selection.switch(x){ case 1: …}
synchronized Modifier Synchronizes threads.synchronized void method(){}
this OOP Refers to current object.this.value = value;
throw Exception Handling Throws an exception.throw new Exception();
throws Exception Handling Declares exceptions.void read() throws IOException{}
transient Modifier Fields not serialized.transient int age;
try Exception Handling Block for exceptions.try { … } catch …
void Data Type Method returns nothing.void print(){}
volatile Modifier Variable may change asynchronously.volatile int counter;
while Control Flow Loop while condition is true.while(x<5){}

Using Java Keywords Correctly

Java keywords are reserved words, which means you cannot use them as variable names, method names, or class names. Doing so will cause a compile-time error. For example:

int for = 5; //This will cause a compile-time error

Instead, use keywords only for their intended purposes. For instance:


int number = 10;       // Correct usage of int
if (number > 5) {      // Correct usage of if
    System.out.println("Number is greater than 5");
}

Tips for using Java reserved keywords correctly:

  • Follow naming conventions: Avoid using names similar to keywords. For example, use className instead of class.
  • Learn by practice: Understanding how each keyword works in loops, conditionals, or class definitions helps avoid errors.
  • Refer to a cheatsheet: A Java keywords cheat sheet can guide you in proper usage and improve coding speed.

Java Keywords Tutorial

Now that we have a solid understanding of all the Java keywords and their purpose, let’s put our learning to the test by building a simple Java program that uses a class, a method, loops, and conditionals:

// A simple program to check and print even numbers
public class KeywordDemo {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5, 6};  // 'int' keyword to declare integer array
        for (int num : numbers) {             // 'for' keyword for looping
            if (num % 2 == 0) {               // 'if' keyword for conditional check
                System.out.println(num + " is even");  // 'System.out.println' prints output
            } else {
                continue;                      // 'continue' skips the rest of the loop iteration
            }
        }
    }
}

Output:

image 88

Explanation:

  • public, class, and static are access and structural keywords.
  • int declares integer variables and arrays.
  • for and if control the program flow.
  • continue allows skipping to the next iteration if the condition isn’t met.
  • System.out.println prints the output, helping us see the result in action.

Conclusion

Java keywords are the backbone of the language. They are reserved words with a specific meaning that the compiler understands, so you can’t use them as variable names, method names, or class names. Using them correctly helps keep your code clean, understandable, and error-free.

In this guide, we looked at what Java keywords are, grouped them into categories, shared a handy cheatsheet, covered some best practices, and even walked through a practical example. Out of the 50 keywords in Java, 48 are actively used; goto and const are reserved but never used.

Mastering these keywords is a small yet powerful step toward writing efficient Java programs. If you want to strengthen your foundation and move beyond the basics, check out our Java full-stack course. It’s designed to help you learn core concepts with hands-on examples and real projects, getting you that much closer to your dream job as a Java developer.

Java Keywords: Complete Guide to Java Reserved Words – FAQs

Q1. Can I use keywords as variable names?

No, you cannot use Java keywords as variable names, method names, or class names. They are reserved words with special meanings, and using them as identifiers will cause a compile-time error.

Q2. What is the difference between final and finally?

final is a keyword used to declare constants, prevent method overriding, or inheritance of a class.
finally is a block in exception handling that always executes after a try-catch, regardless of whether an exception occurred or not.

Q3. What is the difference between static and final?

static makes a variable, method, or block belong to the class rather than instances of the class.
final restricts modification: a final variable cannot be reassigned, a final method cannot be overridden, and a final class cannot be subclassed.

Q4. Which keywords are reserved but not used in Java?

goto and const are reserved keywords in Java, but are never used. They are reserved for potential future use.

Q5. How many keywords are there in Java?

Java has a total of 50 reserved keywords. Out of these, 48 are actively used, and 1 (goto) plus 1 (const) are reserved but not used.

About the Author

Software Developer | Technical Research Analyst Lead | Full Stack & Cloud Systems

Ayaan Alam is a skilled Software Developer and Technical Research Analyst Lead with 2 years of professional experience in Java, Python, and C++. With expertise in full-stack development, system design, and cloud computing, he consistently delivers high-quality, scalable solutions. Known for producing accurate and insightful technical content, Ayaan contributes valuable knowledge to the developer community.

Full Stack Developer Course Banner