Java Keywords

Java programs are made up of different elements such as classes, methods, variables, operators, identifiers, and values. To define how these elements behave, Java provides a set of special words called keywords.

A Java keyword is a word that has a predefined meaning in the Java programming language. Because these words already have a specific purpose, they generally cannot be used as identifiers for variables, methods, classes, or other user-defined names.

For example:

int age = 25;

Here, int is a Java keyword. It tells Java that age is an integer variable.

Similarly:

class Student {
}

Here, class is a keyword that tells Java that Student is a class declaration.

Understanding keywords is essential because they form the basic vocabulary and structure of Java programs.

Installing Java

What Is a Keyword in Java?

A keyword is a reserved word that has a special meaning defined by the Java language.

For example:

public class Student {
}

The words:

public
class

have predefined meanings in Java.

  • public is an access modifier.
  • class declares a class.

You cannot normally redefine their meaning or use them as ordinary identifiers.

For example, this is invalid:

int class = 10;

The compiler rejects it because class is already a Java keyword.

Why Does Java Have Keywords?

Keywords allow Java to understand the structure and meaning of source code.

Consider:

if (age >= 18) {
    System.out.println("Adult");
}

Java recognizes:

  • if as a conditional keyword
  • age as an identifier
  • >= as an operator
  • 18 as a literal
  • { } as a block

The keyword if tells the compiler that a conditional statement is being created.

Without reserved words, the Java compiler would not have a consistent way to distinguish different language constructs.

Keywords vs Identifiers

Keywords and identifiers are different.

A keyword is predefined by Java.

An identifier is a name chosen by the programmer.

For example:

int studentAge = 22;

Here:

ElementExampleMeaning
KeywordintDefines the variable’s type
IdentifierstudentAgeProgrammer-defined variable name
Operator=Assigns a value
Literal22Integer value

Another example:

class Student {
}

Here:

  • class is a keyword.
  • Student is an identifier.

Java Keywords Are Case-Sensitive

Java is case-sensitive, and keywords are written in lowercase.

For example:

class Student {
}

is valid.

But:

Class Student {
}

is not equivalent because Class is not the Java keyword class.

Similarly:

public

is a keyword, while:

Public

is not the same keyword.

Java keywords must therefore be written with their correct spelling and capitalization.

Common Java Keywords

Java has keywords for many areas of the language, including:

  • Class and interface declarations
  • Access control
  • Variables and data types
  • Conditional statements
  • Loops
  • Exception handling
  • Inheritance
  • Object creation
  • Methods
  • Concurrency
  • Modules
  • Packages
  • Type checking

Some commonly encountered Java keywords include:

class
interface
public
private
protected
static
final
abstract
extends
implements
new
this
super
if
else
switch
case
for
while
do
break
continue
return
try
catch
finally
throw
throws
int
long
double
float
char
boolean
byte
short
void
package
import
instanceof
synchronized
volatile
transient
native
strictfp
assert
enum
record
sealed
permits
non-sealed
var

Not all of these have the same role, and some words have special contextual behavior in modern Java. The following sections organize the important ones by purpose.

Data Type Keywords

Java provides keywords for its primitive data types.

These include:

byte
short
int
long
float
double
char
boolean

For example:

int age = 25;

Here, int specifies that age stores an integer value.

Another example:

double price = 99.99;

Here, double specifies a floating-point type.

A character can be declared using char:

char grade = 'A';

A boolean uses:

boolean isActive = true;

The void Keyword

void indicates that a method does not return a value.

For example:

static void displayMessage() {
    System.out.println("Hello Java");
}

The method performs an action but does not return a value.

Class and Object-Related Keywords

Several keywords are used when working with classes and objects.

The class Keyword

The class keyword declares a class.

class Student {

}

Here, class tells Java that Student is a class.

The interface Keyword

The interface keyword declares an interface.

interface Printable {

    void print();
}

Interfaces define contracts that classes can implement.

The new Keyword

The new keyword creates an object or array and allocates the necessary memory.

For example:

Student student = new Student();

Here:

Student  → type
student  → identifier
new      → keyword
Student() → constructor invocation

The this Keyword

this refers to the current object.

For example:

class Student {

    int age;

    void setAge(int age) {
        this.age = age;
    }
}

Here:

this.age

refers to the instance variable belonging to the current object.

The super Keyword

super is used to refer to members of a superclass.

For example:

class Animal {

    void sound() {
        System.out.println("Animal sound");
    }
}

class Dog extends Animal {

    void display() {
        super.sound();
    }
}

super.sound() calls the inherited method from the parent class.

Access Modifier Keywords

Java provides keywords that control the accessibility of classes, methods, and fields.

The main access modifiers are:

public
private
protected

public

public means the member is accessible wherever the applicable Java access rules permit.

Example:

public class Student {

    public void display() {
        System.out.println("Student");
    }
}

private

private restricts access to the class in which the member is declared.

class Student {

    private int age;
}

The age field cannot be directly accessed from unrelated classes.

protected

protected provides access within the same package and also to subclasses under Java’s protected-access rules.

class Animal {

    protected void sound() {
        System.out.println("Animal sound");
    }
}

Modifier Keywords

Java has several keywords that modify the behavior of classes, methods, or variables.

Common examples include:

static
final
abstract
synchronized
volatile
transient
native
strictfp

The static Keyword

static means a member belongs to the class rather than to a particular instance.

For example:

class Calculator {

    static int add(int a, int b) {
        return a + b;
    }
}

The method can be called using the class name:

int result = Calculator.add(10, 20);

The final Keyword

final can be used with variables, methods, and classes.

With a variable, it prevents reassignment after initialization:

final int MAX_USERS = 100;

With a method, final prevents overriding by subclasses.

With a class, final prevents inheritance from that class.

For example:

final class SecurityManager {
}

A class declared final cannot be extended.

The abstract Keyword

abstract is used for abstract classes and methods.

For example:

abstract class Animal {

    abstract void sound();
}

An abstract method does not provide an implementation in the declaration.

The synchronized Keyword

synchronized is used in concurrent programming to control access to code or methods where synchronization is required.

For example:

synchronized void update() {
    // synchronized code
}

It is commonly associated with Java’s multithreading and concurrency mechanisms.

Inheritance Keywords

Java provides keywords for inheritance and implementing interfaces.

The extends Keyword

extends is used when one class inherits from another class.

class Animal {
}

class Dog extends Animal {
}

Here, Dog is a subclass of Animal.

The implements Keyword

implements is used when a class implements an interface.

interface Printable {
    void print();
}

class Document implements Printable {

    public void print() {
        System.out.println("Printing...");
    }
}

Here, Document implements the Printable interface.

Conditional Keywords

Java provides keywords for decision-making.

The main conditional keywords include:

if
else
switch
case
default

if

if executes code when a condition is true.

if (age >= 18) {
    System.out.println("Adult");
}

else

else provides an alternative block when the if condition is false.

if (age >= 18) {
    System.out.println("Adult");
} else {
    System.out.println("Minor");
}

switch

switch can be used when program behavior depends on the value of an expression.

switch (day) {
    case 1:
        System.out.println("Monday");
        break;

    case 2:
        System.out.println("Tuesday");
        break;

    default:
        System.out.println("Other day");
}

case

case defines an individual branch within a traditional switch statement.

default

default defines the branch used when no applicable case matches in a traditional switch statement.

Loop Keywords

Java provides keywords for repeating code.

The primary loop keywords are:

for
while
do

for

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

while

while (count < 5) {
    count++;
}

do

The do keyword is used with while to create a do-while loop.

do {
    System.out.println("Hello");
    count++;
} while (count < 5);

A do-while loop executes its body at least once before checking the condition.

Jump Keywords

Java provides keywords that can change the normal flow of execution.

These include:

break
continue
return

break

break terminates the applicable loop or switch statement.

for (int i = 1; i <= 10; i++) {

    if (i == 5) {
        break;
    }

    System.out.println(i);
}

continue

continue skips the current loop iteration and proceeds with the next iteration.

for (int i = 1; i <= 5; i++) {

    if (i == 3) {
        continue;
    }

    System.out.println(i);
}

return

return exits a method and can provide a return value.

static int add(int a, int b) {
    return a + b;
}

Exception Handling Keywords

Java provides several keywords for handling exceptions.

These include:

try
catch
finally
throw
throws

try

The try block contains code that may throw an exception.

try {
    int result = 10 / 0;
}

catch

catch handles an exception.

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

finally

finally defines code that is intended to execute after the associated try/catch processing, subject to Java’s exception and termination rules.

try {
    System.out.println("Processing...");
} finally {
    System.out.println("Finished.");
}

throw

throw is used to explicitly throw an exception.

throw new IllegalArgumentException("Invalid age");

throws

throws declares exceptions that a method may propagate to its caller.

static void readFile() throws IOException {
    // File operation
}

Package and Import Keywords

Java uses keywords to organize and access code from different packages.

package

The package keyword declares the package to which a source file belongs.

package com.example.student;

Packages help organize related classes and prevent naming conflicts.

import

The import keyword allows a class to refer to types from another package without repeatedly using their fully qualified names.

For example:

import java.util.ArrayList;

You can then use:

ArrayList<String> names = new ArrayList<>();

Type Checking Keyword

instanceof

The instanceof operator checks whether an object is compatible with a particular reference type.

For example:

if (student instanceof Student) {
    System.out.println("Student object");
}

In modern Java, instanceof can also be used with pattern matching:

if (student instanceof Student s) {
    System.out.println(s.getName());
}

The exact pattern-matching features available depend on the Java version being used.

Concurrency-Related Keywords

Java provides several keywords associated with concurrency and memory visibility.

Examples include:

synchronized
volatile

volatile

The volatile keyword tells the Java memory model that reads and writes of a variable have special visibility semantics between threads.

For example:

private volatile boolean running = true;

volatile is useful in certain multithreaded situations, but it does not make compound operations such as count++ automatically atomic.

Assertion Keyword

assert

The assert keyword is used to express assumptions that should normally hold during program execution.

For example:

assert age >= 0;

Assertions are primarily useful for detecting programming errors during development and testing.

Assertions can be enabled or disabled when running a Java application, so they should not normally be used as a replacement for required application validation.

Enum Keyword

enum

The enum keyword is used to declare an enumeration, which represents a fixed set of named constants.

For example:

enum Day {
    MONDAY,
    TUESDAY,
    WEDNESDAY
}

You can then use:

Day today = Day.MONDAY;

Enums are useful when a variable should represent one value from a predefined set.

Modern Java Keywords and Contextual Keywords

Java has continued to evolve over time, so modern Java contains additional language features that beginners may encounter.

Examples include:

record
sealed
permits
non-sealed

Java also has words that have special meaning only in particular contexts. These are often called contextual keywords or restricted identifiers rather than traditional keywords.

Examples include:

var
yield
record
sealed
permits
non-sealed

The distinction matters because not every word that looks like a language keyword has exactly the same lexical status.

The var Keyword

var allows local variable type inference.

For example:

var name = "Karim";

The compiler determines the variable’s static type from its initializer. In this example, name is inferred as a String.

var does not mean that Java becomes dynamically typed.

For example:

var age = 25;

still results in a statically typed local variable whose inferred type is int.

var is primarily used for local variables and has specific restrictions.

The record Keyword

Modern Java provides records for concise declarations of certain kinds of data-carrying classes.

For example:

record Student(String name, int age) {
}

A record automatically provides several members associated with its declared components.

The sealed Keyword

A sealed class restricts which classes can directly extend it.

For example:

sealed class Animal permits Dog, Cat {
}

The permitted subclasses are explicitly listed.

The permits Keyword

permits identifies the classes that are allowed to directly extend or implement a sealed type.

sealed class Animal permits Dog, Cat {
}

Here, Dog and Cat are explicitly permitted subclasses.

The non-sealed Modifier

A permitted subclass of a sealed class can be declared non-sealed to remove the sealing restriction for its own subclasses.

For example:

sealed class Animal permits Dog {
}

non-sealed class Dog extends Animal {
}

These features are part of modern Java’s type-system capabilities.

goto and const

Two words deserve special attention:

goto
const

They are reserved by Java but are not used as normal Java language constructs.

For example:

int goto = 10;

is not allowed.

Similarly:

int const = 10;

is not allowed.

Java uses final for constants rather than a const keyword.

For example:

final int MAX_USERS = 100;

Can Keywords Be Used as Variable Names?

No, Java keywords cannot normally be used as identifiers.

For example:

int class = 10;

is invalid.

Another example:

String public = "Hello";

is also invalid.

Use another identifier instead:

int classNumber = 10;
String message = "Hello";

Keywords vs Reserved Words

In beginner Java discussions, the terms keyword and reserved word are often used interchangeably. However, Java’s language specification distinguishes traditional keywords from certain restricted or contextual keywords.

For everyday Java programming, the important rule is simple:

Do not use Java language keywords or other restricted language words as ordinary identifiers where the language reserves their use.

This distinction becomes more important when studying the Java Language Specification or newer Java features.

Java Keywords Example

The following program demonstrates several common keywords:

public class Student {

    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void display() {

        if (age >= 18) {
            System.out.println(name + " is an adult.");
        } else {
            System.out.println(name + " is a minor.");
        }
    }

    public static void main(String[] args) {

        Student student = new Student("Karim", 22);

        student.display();
    }
}

Some of the keywords used here are:

public
class
private
this
void
if
else
new
static

Each keyword has a specific role in the program.

A Useful Way to Remember Keywords

Instead of trying to memorize every Java keyword immediately, learn them according to their purpose.

CategoryImportant Keywords
Data typesbyte, short, int, long, float, double, char, boolean, void
Classesclass, interface, enum, record
Accesspublic, private, protected
Modifiersstatic, final, abstract, synchronized, volatile
Inheritanceextends, implements
Objectsnew, this, super
Conditionsif, else, switch, case, default
Loopsfor, while, do
Flow controlbreak, continue, return
Exceptionstry, catch, finally, throw, throws
Packagespackage, import
Type checkinginstanceof
Assertionsassert
Modern type featuressealed, permits, non-sealed

Common Beginner Mistakes

Using a Keyword as an Identifier

Incorrect:

int class = 10;

Correct:

int classNumber = 10;

Incorrect Capitalization

Incorrect:

Class Student {
}

Correct:

class Student {
}

Java keywords are case-sensitive.

Confusing this and super

this refers to the current object:

this.age

super refers to the superclass portion of the current object:

super.display();

They have different purposes.

Confusing final With finally

These words look similar but have completely different purposes.

final is a modifier:

final int MAX = 100;

finally is used with exception handling:

try {
    // code
} finally {
    // cleanup
}

Confusing throw and throws

throw actually throws an exception:

throw new IllegalArgumentException();

throws declares that a method may propagate an exception:

void readFile() throws IOException {
}

Frequently Asked Questions

What is a keyword in Java?

A keyword is a word with a predefined meaning in the Java programming language.

Can I use a Java keyword as a variable name?

No. Keywords such as class, int, public, and return cannot normally be used as identifiers.

Is Java case-sensitive?

Yes. Java is case-sensitive, including its keywords.

For example:

class

is the keyword, while:

Class

is not the same keyword.

What is the difference between a keyword and an identifier?

A keyword is predefined by Java, while an identifier is a name chosen by the programmer.

int age = 25;

Here, int is a keyword and age is an identifier.

Is String a Java keyword?

No. String is a class provided by the Java standard library. It is not a Java keyword.

For example:

String name = "Karim";

String is a type name referring to java.lang.String, while name is an identifier.

Is main a Java keyword?

No. main is not a Java keyword.

In:

public static void main(String[] args)

public, static, and void are keywords, while main is the method name.

Is true a Java keyword?

true is a boolean literal rather than a traditional Java keyword, but it is a reserved literal and cannot be used as an ordinary identifier.

The same applies to:

false
null

Is var a Java keyword?

var has special language meaning for local variable type inference, but it is treated as a restricted type name rather than a traditional keyword in the same way as int or class.

to