Special keywords in Java are reserved words that have predefined meanings and cannot be used as identifiers or variable names. These keywords serve specific purposes and are an integral part of the Java language.
The final keyword is used to declare a constant variable, a method that cannot be overridden, or a class that cannot be subclassed.
final dataType VARIABLE_NAME = value; final returnType methodName() { // Method implementation } final class ClassName { // Class implementation }
final double PI = 3.14; final class Circle { final void calculateArea() { // Area calculation implementation } }
The static keyword is used to define a variable, method, or nested class that belongs to the class itself, rather than an instance of the class.
static dataType VARIABLE_NAME = value; static returnType methodName() { // Method implementation } static class NestedClassName { // Class implementation }
static int counter = 0; static void incrementCounter() { counter++; } static class Utility { // Utility class implementation }
The abstract keyword is used to declare an abstract class or method. An abstract class cannot be instantiated, and an abstract method must be implemented by a subclass.
abstract class ClassName { // Class implementation } abstract returnType methodName(); abstract class AbstractClass { abstract void abstractMethod(); void concreteMethod() { // Concrete method implementation } }
abstract class Shape { abstract void draw(); } class Circle extends Shape { void draw() { // Circle drawing implementation } }
The this keyword is a reference to the current instance of a class. It is used to access instance variables, invoke constructors, or differentiate between instance variables and local variables.
this.variableName; this(argumentList); this();
class Person { String name; Person(String name) { this.name = name; } void displayName() { System.out.println("Name: " + this.name); } }
The super keyword is a reference to the superclass of a class. It is used to access superclass members, invoke superclass constructors, or differentiate between superclass members and subclass members.
super.variableName; super(argumentList); super();
class Animal { String type; Animal(String type) { this.type = type; } void displayType() { System.out.println("Type: " + this.type); } } class Dog extends Animal { Dog() { super("Dog"); } }
The instanceof keyword is used to check if an object belongs to a specific class or implements a specific interface.
object instanceof className;
class Circle { // Circle class implementation } Circle circle = new Circle(); boolean isCircle = circle instanceof Circle;