A Complete Guide to Java Keywords
Updated on May 26, 2025 | 19 min read | 6.85K+ views
Share:
For working professionals
For fresh graduates
More
Updated on May 26, 2025 | 19 min read | 6.85K+ views
Share:
Table of Contents
Did you know? Java 24, launched in March 2025, lets you use all primitive types in pattern matching, instanceof, and switch statements!
This preview feature makes your code clearer and more concise. To try it out, just enable it with the --enable-preview flag!
Keywords in Java are reserved words that have a specific meaning and purpose in the language. They can’t be used for naming variables or methods. The problem is, beginners often get confused about what keywords are and how they work.
This article breaks down the meaning of Java keywords and shows you how to recognize and use them correctly.
Improve your coding skills with upGrad’s online software engineering courses. Specialize in cybersecurity, full-stack development, and much more. Take the next step in your learning journey!
Java keywords are special reserved words that the compiler recognizes with a fixed meaning. You cannot use them as names for classes, variables, methods, or identifiers. If you do, the compiler throws an error because these words are part of Java’s language syntax.
The use of Java keywords is more than just a syntax rule, it’s crucial to understand how and when to apply it in different coding scenarios. Proper use ensures your code is clear, efficient, and easier to maintain. Here are three programs that can help you:
There are 51 reserved keywords in Java, plus 16 contextual keywords used in specific situations, totaling 67. These words guide the compiler on how to interpret your code and cannot be redefined by programmers.
How Java keywords work:
Recent Additions:
Java has recently added some new Java language keywords for its development. These additions help improve code safety, readability, and maintainability in modern Java development:
Unused Reserved Keywords:
Java reserves certain keywords for potential future use, even though they are not actively used in the language. These reserved words cannot be used as identifiers, ensuring compatibility with possible future updates. While they have meanings in other programming languages, Java intentionally avoids using them to promote better coding practices and maintain clarity:
Avoid using reserved keywords like goto and const as names, they’ll cause errors. Use final instead to create constants. Next, let’s look at the key categories of Java keywords and how they shape your code.
Java Keywords can be grouped into different categories based on their purpose in the language. These keywords help define data types, control program flow, handle exceptions, manage classes and objects, and more. The following is a comprehensive Java keyword guide with descriptions.
Access modifiers define who can access different parts of your code in Java. They help protect data and methods from accidental use or modification. By using access control, you determine how different parts of your program interact, making it more secure and organized.
In simpler terms, these can be viewed as visibility settings. The following table provides an overview of Java access modifiers:
Keywords |
Description |
public |
Indicates that members of any class can access a method, variable, class, or interface. |
protected |
Specifies that a method or variable is accessible within its class, subclass, or classes from another package. |
private |
Restricts access so that only the class in which the method or variable is declared can access it. |
In Java, special keywords modify the behavior of classes, methods, and variables. These modifiers help organize code and define certain behaviors, such as making methods belong to a class, preventing variables from being changed, or requiring subclasses to implement abstract methods.
Below is a detailed look at Java keywords in this category:
Keywords |
Description |
abstract |
Declares a class or method as abstract. A class that contains a method without a definition is called an abstract class, whereas the method itself is called an abstract method. When a class or method is declared as abstract, it indicates that the subclass will implement it later. |
static |
Declares a method or variable as a class-level member instead of restricting it to a specific object. |
final |
This prevents modification. A final class cannot be subclassed, a final variable can only contain a constant value and a final method cannot be overridden. |
strictfp |
Ensures consistent floating-point calculations by limiting accuracy and rounding variations for portability. |
synchronized |
Defines critical sections in multithreaded Java programs to prevent concurrent access issues. |
volatile |
Indicates that a variable’s value may change asynchronously, often used in multithreading. |
transient |
Used in serialization to specify that a data member should not be serialized. |
native |
Specifies that a method is implemented in native code using the Java Native Interface (JNI). |
Also Read: Types of Variables in Java: Java Variables Explained
Control flow statements guide a program's decision-making and repetition processes. They enable code to execute specific sections based on conditions or repeat actions multiple times, ensuring the program responds appropriately to different scenarios.
The following table provides an overview of Java keywords used in control flow statements:
Keywords |
Description |
if
|
In order to test a boolean expression, it is used to specify an if statement. For instance:
|
It is also used to test a boolean expression in combination with the if statement. For instance:
|
|
switch |
It runs code based on the test value. Once the test value matches each case inside the switch statement, it executes the corresponding test case. |
case |
This term is used inside the switch statement to indicate different matching cases. However, inside the switch statement, it also indicates a block of text. As an illustration, consider this:
|
default |
The switch statement, which is run when no case matches the provided value, can optionally employ it. As an illustration, consider this:
|
for |
It is employed in programs to loop through a collection of statements. It is employed to initiate a loop. As an illustration, consider the following:
|
Depending on whether a given condition is true or false, it is utilized to carry out a block of statements. As an illustration:
|
|
do |
In Java, it creates a do-while loop when combined with the while loop. As an illustration, consider this:
|
break |
It is a control statement that is used to exit a loop and interrupt its execution. As an illustration, consider this:
|
continue |
It is used to return control to the loop while ignoring all subsequent statements in a program. As an illustration:
|
return |
It is used to return a value to the caller method and to indicate that a method has finished running. An example:
|
assert |
This keyword enables programmers to confirm the assumptions stated in a program rapidly. |
If you're still building your Python skills, now is the perfect time to strengthen that foundation. Check out the Programming with Python: Introduction for Beginners free course by upGrad to build the foundation you need before getting into programming.
Also Read: Python Break, Continue & Pass Statements [With Examples]
Java provides mechanisms to handle problems that may arise during execution. Programs can catch errors, manage them efficiently, and properly handle resources through specific constructs. This helps prevent crashes and ensures smooth execution. The following table provides an overview of Java keywords related to error or exception handling:
Keywords |
Description |
Specifies a block of code that will be examined for handling exceptions. |
|
catch |
This keyword works in tandem with an optional finally block and the try block. It is used to catch an exception and define what to do when the try block throws an exception. |
finally |
Refers to a block that is always executed when used with the try-catch structure. |
throw |
Used to throw exceptions, mostly custom exceptions explicitly. However, an instance of an exception must follow it. |
throws |
Declares exceptions in Java and specifies which exceptions a method may throw. |
The foundation of Java is object-oriented programming (OOP). This approach to software design centers around data and the operations applied to it. It promotes modularity, reusability, and a clean structure, making complex software systems more manageable. Below is an overview of Java keyword meanings:
Keywords |
Description |
class |
Used to define a class. A class in Java represents a specific type of object.
|
interface |
Creates a class-like structure that contains only static interfaces, final fields, and abstract methods. Other classes can later implement these interfaces. |
extends |
Indicates that a class is inheriting from another class or interface. Used in class definitions. For example, class A extends B. |
implements |
Specifies the interfaces that a class implements. For example, class A implements InterfaceB. |
new |
Used to create new instances of a class. |
this |
Refers to the current instance of the class in which it is used. |
super |
Refers to the superclass (base class) of the current class. It can be used with constructors and methods. |
instanceof |
Check whether an object is an instance of a specific class or implements an interface. |
Dealing with complex coding structures can be overwhelming without the right foundation. Explore upGrad’s free Java Object-oriented Programming course to sharpen your skills and streamline your coding process. Start now!
These keywords help organize classes and interfaces into packages, similar to folders in a file system. Package management prevents naming conflicts, improves code maintainability, and enables developers to manage access control and namespaces effectively. This results in cleaner, more efficient code.
The table below lists commonly used Java programming keywords for package management:
Keywords |
Description |
package |
A Java package is declared using this keyword. A collection of classes and interfaces is called a package. An example of a package: package com.bookstore.app;
|
import |
This keyword is used to include classes or entire Java packages so they can be referenced later in the application without explicitly mentioning the package name. Example: import java.util.Scanner; |
Java provides eight basic or primitive data types. These include int for whole numbers and double for decimals. A char represents a single character, and a boolean stores true or false values. These are the building blocks for manipulating data in the language.
The following table provides a category-wise explanation of Java keywords related to primitive data types:
Keywords |
Description |
byte |
8-bit values can be stored in this keyword's data type. Example: byte aByte = 100; |
short |
Used as a data type that can store a 16-bit integer. Example: short aShort = 10000; |
int |
Stores a 32-bit signed integer and is used as a data type in Java. Example: int a = 5; |
long |
Stores a 64-bit integer as a Java data type. Example: long aLong = 100000L; |
float |
Stores a 32-bit floating-point number. Example: float aFloat = 10.5f; |
double |
Stores a 64-bit floating-point value. Example: double aDouble = 20.99; |
char |
Stores any character defined in the Java character set. Example: char aChar = 'A'; |
boolean |
Used to define a boolean variable, which accepts only true or false values. Its default value is false. Example: boolean aBoolean = true; |
Use upGrad's thorough tutorials on Java keywords and other topics to improve your Java skills. Explore our comprehensive Java tutorials to become an expert Java programmer.
If you're finding it tough to break down complex problems efficiently, check out upGrad’s Data Structures & Algorithms free course to strengthen your foundation and start solving challenges with ease. Start today!
Also Read: What is Type Conversion in Java? [With Examples]
remember that mastering the java keywords list helps you write clear and error-free code. Always keep track of what is keyword in java and use each keyword according to its purpose to avoid common pitfalls. Some words act as contextual keywords in Java, gaining special meaning only in certain situations.
These are also known as restricted Java identifiers and restricted keywords. They give the code specific meaning. Contextual keywords follow Java keyword restrictions and are interpreted based on their syntactic grammatical position.
When learning what is keyword in java, it’s important to know about more than just the core java keywords. Some keywords, known as contextual keywords in Java, behave differently. These are special words that only act as keywords in certain situations. Outside those specific contexts, they work like regular identifiers or names.
This flexibility helps keep the java keywords list from growing too large, making the language easier to manage without losing expressiveness.
The context in which the following 16 words appear determines whether they are considered keywords or other tokens. The following table shows the Java keyword roles as contextual keywords:
Keywords |
Description |
exports |
modules are imported and exported using exports. |
module |
This keyword is used to declare a module. |
non-sealed |
It is used to specify sealed classes and interfaces that are not sealed. |
open |
It is used for module declaration. |
opens |
These are used for module import and export. |
permits |
It enables for defining of interfaces and sealed classes. |
provides |
It also allows the modules to be imported and exported. |
record |
It is used to define new records. |
requires |
This is the specification for module import and export. |
sealed |
It is used to specify interfaces and classes that are sealed. |
to |
It is also utilized for module import and export. |
transitive |
In a RequiresModifier, it is identified as a terminal. |
uses |
Modules are imported and exported using it. |
var |
It is employed to determine the types of local variables. |
with |
Modules are imported and exported using it. |
yield |
In a switch statement, it is employed to produce a value. |
Contextual keywords improve code readability by making syntax more intuitive and self-explanatory. Since they function as keywords only in specific scenarios, they prevent conflicts with existing identifiers, ensuring smoother transitions when new language features are introduced. This approach reduces ambiguity while maintaining backward compatibility.
When you understand what is keyword in java, you’ll see that using contextual keywords in Java offers several clear advantages over adding more globally reserved java keywords.
Existing code using var as an identifier remains unaffected outside the new context, protecting legacy projects from breaking changes.
For instance, record is a contextual keyword for defining compact data classes (introduced in Java 16). But outside that use, you can name variables or methods record without causing compiler errors.
This approach enabled modern language features without requiring massive syntax changes or disrupting the existing java keywords list.
Also Read: Exploring the 14 Key Advantages of Java: Why It Remains a Developer's Top Choice in 2025
Remember that these special words add flexibility without bloating the java keywords list. Knowing what is keyword in java means understanding not just the core keywords but also how context changes their meaning.
The default keyword is a great example, as it plays different roles in Java’s control flow and interfaces.
The default keyword is primarily used in switch statements. It enables a variable to be compared against a list of values to check for equivalence. Each value is referred to as a case, and each case involves checking the variable being evaluated.
A special case that executes without requiring a matching value is represented by default. If a break or exit statement appears before the default statement in any of the previous cases, it will prevent the default case from being executed. The default case is optional.
The default keyword has two main uses in Java:
1. In switch statements:
It defines a block of code that runs if none of the cases match the tested value.
The default case is optional but ensures your code handles unexpected values gracefully. If a break or return appears before the default case in previous cases, the default block won’t run.
2. In interfaces (since Java 8):
Before Java 8, interfaces could only declare abstract methods without implementations. Adding a new method meant changing every implementing class.
Java 8 introduced default methods, allowing interfaces to include method implementations. This lets you add new methods without breaking existing classes.
The syntax of a default method is as follows:
public interface Example {
// Default method with an implementation
default void print() {
System.out.println("This is a default method!");
}
}
Now let’s see a working example of a default method in Java:
interface MyExampleInterface
{
public void cube(int num); // abstract method
default void print() // default method
{
System.out.println("This is a default method!");
}
}
class MyClass implements MyExampleInterface
{
// implementation of cube abstract method
public void cube(int num)
{
System.out.println(num*num*num);
}
public static void main(String args[])
{
MyClass obj = new MyClass();
obj.cube(5);
obj.print(); // default method executed
}
}
The output of the above-mentioned code is as follows:
125
This is a default method!
The default keyword is a powerful part of Java, helping you handle unmatched cases in control flow and add flexibility to interfaces. Similarly, the ‘this’ keyword plays a crucial role in managing object references and clarifying scope within your code.
The ‘this’ keyword in Java refers to the current object, the instance on which a method or constructor is called. It helps you distinguish between class fields and parameters or clarify which object you’re working with.
Understanding ‘this’ is essential because it avoids confusion when variable names overlap and makes your code easier to read and maintain.
Understanding the use of this keyword in java helps prevent common bugs and makes your code easier to read and maintain.
A common situation where this is needed is when constructor or method parameters have the same names as instance variables. Without this, assignments might update the parameter instead of the actual class field.
public class Person {
private String name;
public Person(String name) {
this.name = name; // 'this.name' refers to the instance variable, 'name' is the parameter
}
}
Here, this.name ensures the class’s field is correctly assigned.
Java allows you to call one constructor from another within the same class with the use of this keyword in Java. This technique helps avoid repeating code and keeps constructors clean and organized.
public class Person {
private String name;
private int age;
public Person() {
this("Unknown", 0); // Calls the parameterized constructor
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
Use of this keyword in java in the no-argument constructor delegates initialization to the parameterized constructor, improving code reuse.
The use of this keyword in Java, the pass the current object as a parameter to other methods or constructors, making your code more flexible.
public class Printer {
public void printPerson(Person person) {
System.out.println("Person's name: " + person.getName());
}
}
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public void print() {
Printer printer = new Printer();
printer.printPerson(this); // Passing the current object
}
public String getName() {
return name;
}
}
This example demonstrates the use of this keyword in java by passing the current instance of Person to the Printer class.
Understanding the use of this keyword in java is essential for writing clear and bug-free code, especially when dealing with object references and constructor logic. Learning this concept lays the groundwork for how to implement keywords in Java effectively throughout your programs.
Before diving into how to implement all the java keywords effectively, it’s important to understand what is keyword in java and be familiar with the full java keywords list. Without this foundation, it’s easy to misuse keywords, causing errors or confusing your code.
Having a clear understanding of the purpose and rules of each keyword will help you apply them correctly, write cleaner code, and avoid common pitfalls.
The following list of tips and tricks can enhance your Java keyword usage.
While programming in Java, it is important to be aware of common pitfalls that can impact performance and maintainability. Here are some mistakes to avoid:
Making use of the right tools may improve your proficiency in using Java keyword definitions correctly.
Testing your code often catches mistakes related to keyword misuse early. Practice writing small programs that use various keywords. Review and improve your code regularly to build confidence and deepen your understanding. Keep exploring and applying these concepts to become a stronger Java developer.
Java has a rich set of keywords that define its structure. Keywords like class, public, static, and new form the base of object-oriented programming. Control flow is handled using if, else, switch, while, and for, while try, catch, and finally manage exceptions.
But while knowing them is necessary, many learners struggle with applying them meaningfully in real-life projects and interviews.
To help bridge this gap, upGrad’s personalized career guidance can help you explore the right learning path based on your goals. You can also visit your nearest upGrad center and start hands-on training today!
Boost your career with our popular Software Engineering courses, offering hands-on training and expert guidance to turn you into a skilled software developer.
Master in-demand Software Development skills like coding, system design, DevOps, and agile methodologies to excel in today’s competitive tech industry.
Stay informed with our widely-read Software Development articles, covering everything from coding techniques to the latest advancements in software engineering.
References:
https://www.linkedin.com/pulse/rising-demand-java-developers-today-aspire-techsoft-0schf/
https://www.linkedin.com/pulse/java-keywords-reserved-words-nareshit-naresh-i-technologies-43jhc/
https://devblogs.microsoft.com/java/the-state-of-coding-the-future-with-java-and-ai/
900 articles published
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology s...
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources