Keywords in Java

List of Java Keywords

April 7th, 2026
12223
7:00 Minutes

Java provides a comprehensive list of reserved words known as keywords. Keywords in Java have a specific meaning to the Java compiler. They are fundamental to the language's syntax and structure and can not be used as other identifiers in Java like variable names, method names or class names. This article explains each of them with their specific meaning and use case. It also includes an example on how to use keywords in Java.

What are Keywords in Java?

Keywords in Java are the predefined elements that can be used in a code for a specific meaning only. It is also important to ensure that these are used accurately due to Java's case-sensitive nature. This means developers can not use them directly but can customize them by adding a number or changing their letters to define any other variable, method or class.

Explore igmGuru's Java Course program to learn Java from experts.

Types of Keywords in Java

Java keywords can be categorized based on their role in the language. Grouping them into different types makes it easier to understand their purpose and helps developers learn Java syntax more effectively. The following sections explain the major categories of keywords in Java along with their usage.

Access Modifier Keywords

Keyword Usage
public Allows access from any class.
private Restricts access to within the same class.
protected Allows access within the package and subclasses.

Data Type Keywords

Keyword Usage
boolean Stores true or false values.
byte Stores an 8-bit signed integer.
short Stores a 16-bit signed integer.
int Stores a 32-bit signed integer.
long Stores a 64-bit signed integer.
float Stores a single-precision floating-point number.
double Stores a double-precision floating-point number.
char Stores a Unicode character.
void Indicates that a method does not return any value.

Class and Object-Oriented Programming Keywords

Keyword Usage
class Declares a class.
interface Declares an interface.
extends Enables inheritance between classes or interfaces.
implements Specifies that a class implements an interface.
this Refers to the current object.
super Refers to the parent class.
new Creates objects and arrays.
abstract Declares abstract classes or methods.
final Prevents inheritance, overriding, or reassignment.
static Belongs to the class rather than an instance.
native Declares methods implemented in another language.
record Declares a record class.
sealed Restricts inheritance to specific classes.
permits Lists permitted subclasses of a sealed class.

Control Flow Keywords

Keyword Usage
if Executes code when a condition is true.
else Executes alternative code when a condition is false.
switch Selects one block from multiple conditions.
case Defines a branch inside a switch statement.
default Executes if no switch case matches.
for Creates a loop with initialization, condition, and update.
while Repeats execution while a condition is true.
do Executes code at least once before checking a condition.
break Terminates a loop or switch statement.
continue Skips the current iteration of a loop.
return Exits a method and optionally returns a value.
yield Returns a value from a switch expression.

Exception Handling Keywords

Keyword Usage
try Defines a block where exceptions may occur.
catch Handles exceptions thrown in a try block.
finally Executes regardless of whether an exception occurs.
throw Explicitly throws an exception.
throws Declares exceptions that a method may throw.

Package and Module Keywords

Keyword Usage
package Declares a package.
import Imports classes and packages.
module Defines a module.
requires Declares a dependency on another module.
exports Makes a package accessible to other modules.
open Opens an entire module for reflection.
opens Opens a specific package for reflection.
uses Declares service usage.
provides Provides a service implementation.
with Specifies a service implementation class.
to Restricts package access to specific modules.
transitive Exposes module dependencies transitively.

Concurrency and Memory Management Keywords

Keyword Usage
synchronized Ensures only one thread accesses code at a time.
volatile Ensures visibility of variable updates across threads.
transient Excludes a field from serialization.
strictfp Ensures consistent floating-point calculations.

Literal Keywords

Keyword Usage
true Represents the boolean value true.
false Represents the boolean value false.
null Represents the absence of an object reference.

Reserved but Unused Keywords

Keyword Usage
const Reserved but not implemented in Java.
goto Reserved but not implemented in Java.
_ Reserved for future use.

Impact of Keywords on Java Programs

Java keywords are not just syntactic elements. They actively shape how programs behave, perform and interact with system resources. Understanding their impact is essential for writing robust, efficient, and maintainable Java applications.

1. Influence on Program Logic and Flow

Keywords such as if, else, switch, case, break, continue, and return are fundamental in directing program execution. Their correct placement determines which code blocks run and under what conditions.

  • Logic Errors: Misusing control flow keywords can lead to unexpected behaviors. For example, a misplaced break might cause a loop or switch statement to exit prematurely, skipping critical logic. Similarly, an incorrect continuation can bypass necessary computations within a loop, resulting in subtle bugs that are hard to trace.
  • Unreachable Code: Using return in the wrong place may cause subsequent code to become unreachable, which not only triggers compiler warnings but can also hide logic errors.

2. Impact on Concurrency and Thread Safety

Certain keywords are critical for managing concurrent execution and ensuring thread safety:

  • synchronized: Restricts access to a block of code or method so that only one thread can execute it at a time. This prevents race conditions but, if overused, can create performance bottlenecks due to thread contention.
  • volatile: Ensures that changes to a variable are immediately visible to all threads. Without volatile, threads may cache variables locally and work with outdated values, leading to unpredictable results.
  • transient: While primarily related to serialization, marking a field as transient ensures that sensitive or non-thread-safe data is not serialized and shared unintentionally across threads or distributed systems.

3. Effect on Serialization

Serialization in Java allows objects to be converted into a byte stream for storage or transmission. The transient keyword plays a vital role here:

Excluding Fields from Serialization: Fields marked as transient are skipped during serialization. This is important for excluding sensitive data (like passwords) or fields that are not serializable, preventing errors or security risks.

4. Performance Considerations

Some keywords have a direct or indirect impact on program performance:

  • final: Declaring variables or methods as final allows the Java Virtual Machine (JVM) to make certain optimizations, such as inlining methods or caching constants.
  • synchronized: While essential for thread safety, excessive use can slow down applications due to increased locking overhead.
  • volatile: Ensures visibility but may reduce performance compared to non-volatile variables because it prevents certain compiler and CPU optimizations.

5. Real-World Consequences of Keyword Misuse

Misapplying or misunderstanding keywords can lead to:

  • Compilation Errors: Attempting to use a reserved keyword as a variable or method name results in immediate errors.
  • Logic Bugs: Incorrect control flow keywords can cause unexpected execution paths.
  • Security Flaws: Failing to mark sensitive fields as transient may expose private data during serialization.
  • Concurrency Issues: Neglecting to use synchronized or volatile when necessary can result in race conditions or data inconsistency.

Deprecated or Obsolete Keywords in Java

Java has a few keywords that still exist in the language specification but are rarely used or no longer relevant in modern Java development. These keywords are not removed to maintain backward compatibility, but developers are generally advised to avoid them in new applications. Here are some of them:

  • goto and const: These are reserved keywords in Java, but they have never been implemented. You cannot use them as identifiers, yet Java provides no actual functionality for them. They were reserved early to avoid conflicts with future language features.
  • strictfp: It was introduced to ensure consistent floating-point calculations across different platforms. However, strict floating-point behavior is applied by default. This makes strictfp practically obsolete.

Understanding these obsolete or unused keywords helps learners avoid confusion and focus on keywords that actually matter in real-world Java programming.

Contextually Reserved Words vs Fully Reserved Keywords in Java

Not all keywords of Java behave in the same way. Some words are fully reserved keywords, while others are contextually reserved. In simple terms, they act like keywords only in specific situations.

  • Fully reserved keywords

Keywords like class, public, static, and void are always treated as keywords. You cannot use them as variable names, method names, or class names anywhere in your code. These are called fully reserved keywords.

  • Contextually reserved words

Keywords like module, requires, exports, opens, and permits are treated as keywords only when used in specific contexts. They behave differently. Examples of contexts are module declarations or sealed classes. Outside those contexts, they can still be used as identifiers.

Real-World Example of Using Keywords in Java

Now that you know each type of keyword in Java, let's explore an example on how to use them:


// Define a public class named 'GreetingApp'
public class GreetingApp {

    // Define the main method, which is the entry point of the application
    public static void main(String[] args) {

        // Print a greeting message to the console
        System.out.println("Hello, Java Keywords!");
        System.out.println("Welcome to a basic Java program.");

        // Call another method defined within the same class
        displayCurrentTime();
    }

    // A private helper method to display the current time
    private static void displayCurrentTime() {

        // You would typically use java.time.LocalDateTime here for modern date/time
        // For simplicity and focusing on keywords, let's just print a placeholder
        System.out.println("Current Time: [Placeholder for actual time]");
    }
}

This code example prints messages to your console. It demonstrates fundamental concepts like defining a class, the entry point and how to display output. It is a foundational example for anyone starting with the Java programming language. The following keywords are used in this code:

  • public: This access modifier helps to make sure that classes, methods or variables are accessible from any other class. It is important for the Java Virtual Machine (JVM) to start the program.
  • class: This keyword is used to declare a class and works as a blueprint for creating objects. Nearly all code of this programming language resides within classes.
  • static: This non-access modifier means a member belongs to the class itself, rather than to any specific object of that class. You can call static members directly using the class name.
  • void: As a return type, void indicates that a method does not return any value after it has finished executing.
  • main: Although not a keyword, main is the specific method name the JVM looks for to begin the execution of a standalone Java application. It must always have the signature public static void main(String[] args).

Wrapping Up

We have explored all keywords in Java in this article along with a hands-on example. Now you know how to use them effectively in code to build a robust application. Well, this is not it, as you have to learn many more concepts. Continue to explore our Java tutorial to build a strong foundation in this programming language.

Explore Our Related Articles

FAQs

Q1. Why do we use keywords in Java?

These are used to

Q2. What is the super keyword in Java?

The super keyword is a variable or immediate parent class object. It is often used in subclasses to access members of the superclass. It is mostly used when members are overridden in the subclass.

Q3. How do you define a constructor?

A constructor is a special method that is automatically called when you create an object. It is responsible to set the initial state of an object. It does so by assigning values to its attributes or performing other setup operations.

Q4. How do keywords differ from reserved words in Java?

Keywords are special words in Java that are used to perform specific tasks. Reserved words are words that Java keeps aside and cannot be used as identifiers even if they are not currently in use.

Q5. Why are keywords important in Java?

Keywords define the structure and behavior of Java programs and help the compiler understand the code.

Course Schedule

Course NameBatch TypeDetails
Java TrainingEvery WeekdayView Details
Java TrainingEvery WeekendView Details
About the Author
Author Nehal Sharma
About the Author

Nehal Sharma is a skilled Data Analyst with expertise in Java, mobile development, and data analytics. She transforms complex data into actionable insights and has experience in business intelligence, data science, and Salesforce. She also simplifies technical concepts into clear, engaging content for learners and professionals.

Drop Us a Query
Fields marked * are mandatory
×

Your Shopping Cart


Your shopping cart is empty.