Java Reserved and Contextual Keywords

Java has 51 reserved words that have very specific meanings and cannot be used as identifiers in the application code. Also, 16 contextual keywords are treated as keywords when found in a specific context. Programmers should not use these keywords for anything other than what they are meant to be.

1. What is a Keyword in Java?

The keywords are predefined, reserved words that have a very specific meaning for the compiler. These keywords cannot be used as variables, methods, classes, or any other identifiers.

In the following statement, int is a keyword that indicates that the variable age is of integer type (32-bit signed two’s complement integer). We can’t use int as a variable name etc. Using keywords as identifiers will result in compilation errors.

int age;
  • The keywords const and goto are reserved, even though they are not currently used.
  • true, false, and null might seem like keywords, but they are literal; we cannot use them as identifiers in our programs.
  • strictfp was added in JDK 1.2.
  • assert was added in JDK 1.4.
  • enum was added in JDK 1.5.
  • Later versions of features such as sealed classes, records and JPMS added few more contextual keywords.

2. Java Keywords

The following 51 keywords cannot be used as identifiers.

KeywordDescription
abstractUsed with classes and methods. An abstract class cannot be instantiated. An abstract method is incomplete without the body and must be implemented in the child class to create an instance of the child class.
assertenables us to test the assumptions about our program.
booleanrepresents only one of two possible values i.e. either true or false.
breakis used to terminate forwhile, or do-while loop. It may also be used to terminate a switch statement as well.
bytecan store whole numbers from -128 and 127.
caserepresents a block of code in switch statements.
catchfollows the try block and handles the checked exceptions thrown by try block and any possible unchecked exceptions.
charused to store a single character.
classdefines a class.
constis a reserved keyword for constant values. Use final instead.
continueskips the current iteration of a forwhile, or do-while loops and jumps to the next iteration.
defaultused to specify the default block in a switch statement and default methods in functional interfaces.
doused to contain the statements to execute repeatedly until the condition in the while statement is true.
doubleused to declare a variable that can hold 64-bit floating-point number.
elseused to indicate the alternative branches in an if statement.
enumis a type whose fields consist of a fixed set of constants.
extendsused for extending a class.
finalused with class variables, methods or classes. A final variable cannot be assigned another value after it has been initialized. A final method cannot be overridden in the child class. No class can subclass a final class.
finallycontains code to be executed everytime a try-catch block is completed – either with errors or without any error.
floatused to declare a variable that can hold a 32-bit floating-point number.
forstart a loop to execute a set of instructions repeatedly when a condition is true. If the number of iterations is known, it is recommended to use for loop.
gotoCurrently, not in use.
ifused for writing conditional statements.
implementsused for implementing an interface.
importimport a package, class or interface to the current class.
instanceofChecks whether an object is an instance of a specific class or an interface.
intused to store a 32-bit integer value.
interfacedeclares an interface.
longused to store a 64-bit integer value.
nativeindicates native code (platform-specific).
newcreates a new object of the specified class.
packagedeclares a package for storing the related classes.
privateaccess modifier to indicate that a method or variable may be accessed only in the class in which it is declared.
protectedaccess modifier to indicate that a class, method or variable may be accessed only in the current package, or inherited outside the current package.
publicaccess modifier to indicate that a class, method or variable is accessible everywhere.
returnused to return from a method when its execution is complete.
shortused to store a 16-bit integer value.
staticindicates that a variable or method belongs to the class object, not to the individual instances of that class.
strictfpused to restrict the floating-point calculations to ensure portability.
superused to refer to parent class objects.
switchhelp in providing multiple possible execution paths for a program.
synchronizedmarks a block or method a critical section where one and only one thread is executing at a time.
thisused to refer to the current object.
throwused to explicitly throw an exception from a method or constructor. 
throwsused to declare the list of exceptions that may be thrown by that method or constructor.
transientused on class attributes/variables to indicate that the serialization process of this class should ignore such variables.
trycontains the application code which is expected to work in normal conditions.
voidspecifies that a method should not have a return value.
volatileindicates that an attribute is not cached thread-locally, and is always read from the “main memory”.
whilecontinually executes a block of statements until a particular condition evaluates to true
_ (Underscore)added in Java 9, to prevent writing underscores as an unused lambda, method, or catch formal parameter.

3. Contextual Keywords

The following 16 words can be interpreted as keywords or as other tokens, depending on the context in which they appear.

KeywordDescription
exportsused for importing and exporting the modules.
moduleused for declaring modules.
non-sealedused to define sealed classes and interfaces.
openused for declaring modules.
opensused for importing and exporting the modules.
permitsused to define sealed classes and interfaces.
providesused for importing and exporting the modules.
recordused to define new
requiresused for importing and exporting the modules.
sealedused to define sealed classes and interfaces.
toused for importing and exporting the modules.
transitiverecognized as a terminal in a RequiresModifier.
usesused for importing and exporting the modules.
varused to infer local variable types.
withused for importing and exporting the modules.
yield used to yield a value in a switch statement.
  • The module and open are used for declaring modules.
  • The exportsopensprovidesrequirestouses, and with are used for importing and exporting the modules.
  • The transitive is recognized as a terminal in a RequiresModifier.
  • The var is used to infer local variable types.
  • The yield is used to yield a value in a switch statement.
  • The record is used to define new record types.
  • The non-sealedpermits, and sealed are used to define sealed classes and interfaces.

Learning about all keywords on a single page is not possible. We will learn about each Java keyword in its dedicated tutorial.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.