Back to Study Material Java

Java Keywords

Keywords in Java are reserved words that have predefined meanings defined by the language specification. They cannot be used as identifiers (variable names...

Java Keywords

Reserved Words That Give Java Its Structure

Keywords in Java are reserved words that have predefined meanings defined by the language specification. They cannot be used as identifiers (variable names, class names, method names, etc.). Java has 51 reserved keywords, and each one signals a specific construct or behavior to the compiler. Knowing the most important keywords and their purposes is necessary for reading and writing Java code correctly.

Access modifier keywords control visibility. public makes a member accessible from any class. private restricts access to within the same class only. protected allows access from the same class, the same package, and subclasses. The default (no keyword) allows access within the same package only.

Class structure keywords define the skeleton of object-oriented code. class declares a new class. interface declares an interface. extends indicates that a class inherits from another class. implements indicates that a class fulfills an interface contract. abstract declares a class or method that must be subclassed or overridden. final on a class prevents inheritance; final on a method prevents overriding; final on a variable makes it a constant.

Type-related keywords include void (no return value), new (creates a new object), instanceof (checks object type), and static (class-level rather than instance-level binding).

Flow control keywords guide program execution. if, else, switch, case, default, break, and continue all appear in branching and looping constructs. for, while, and do initiate loops. return exits a method and optionally sends back a value. throw signals that an exceptional condition has occurred. try, catch, and finally form the exception handling structure.

The this keyword refers to the current object instance, useful for distinguishing instance variables from method parameters with the same name: this.name = name;. The super keyword refers to the parent class, used to call parent constructors (super()) or parent methods (super.display()).

Other notable keywords include synchronized (thread safety), volatile (prevents thread caching of a variable), transient (excludes a field from serialization), and native (declares a method implemented in native code via JNI).

Interview tip: The words const and goto are reserved in Java but have no current use — they were reserved to prevent their use as identifiers and to potentially use them in future versions. Also note that true, false, and null are not keywords but literals; they are also reserved and cannot be used as identifiers.