Final Keyword In Java

In Java, the “final” keyword is used to create a constant variable or a final method or a final class, which cannot be modified once they have been assigned a value.

When applied to a variable, “final” makes it a constant, meaning that its value cannot be changed. Once a final variable is initialized, it cannot be reassigned.

When applied to a method, “final” makes it impossible to override the method in a subclass. This is useful when the behavior of a method in a superclass should not be changed by subclasses.

When applied to a class, “final” makes it impossible to extend that class, meaning that no subclasses can be created. This is useful when a class should not be modified or extended.

Here’s an example of how “final” can be used to create a constant variable:

final int MAX_VALUE = 100;

Here’s an example of how “final” can be used to create a final method:

public class SuperClass {
   final void myMethod() {
      System.out.println("This method cannot be overridden.");
   }
}

public class SubClass extends SuperClass {
   // This will result in a compile-time error
   void myMethod() {
      System.out.println("This method cannot be overridden.");
   }
}

Here’s an example of how “final” can be used to create a final class:

final public class MyClass {
   // Class implementation
}

// This will result in a compile-time error
public class MySubClass extends MyClass {
   // Class implementation
}

In summary, the “final” keyword in Java can be used to create a constant variable, a final method, or a final class. These constructs cannot be modified once they are assigned or defined.

1) Java final variable:

In Java, a final variable is a variable that can only be assigned a value once, and once assigned, the value cannot be changed.

To declare a final variable, you use the “final” keyword before the variable type. For example:

final int x = 5;

Here, “x” is a final variable that has been assigned the value 5. It cannot be reassigned to another value.

A final variable must be initialized before it can be used. This can be done at the time of declaration, or in the constructor of the class in which it is declared.

Final variables are typically used for values that should not change during the execution of the program, such as constants or configuration settings. By making these variables final, you can ensure that they are not accidentally changed, which can cause unexpected behavior in the program.

Here’s an example of how a final variable can be used in Java:

public class MyClass {
    final int MAX_SIZE = 10;
    
    public void myMethod(int[] values) {
        if (values.length > MAX_SIZE) {
            // Do something
        } else {
            // Do something else
        }
    }
}

In this example, “MAX_SIZE” is a final variable that has been assigned the value 10. It is used to check the length of the “values” array in the “myMethod” method. Since “MAX_SIZE” is a final variable, its value cannot be changed during the execution of the program.

2) Java final method:

In Java, a final method is a method that cannot be overridden by a subclass.

To declare a final method, you use the “final” keyword before the method signature. For example:

public class SuperClass {
    public final void myMethod() {
        // Method implementation
    }
}

public class SubClass extends SuperClass {
    // This will result in a compile-time error
    public void myMethod() {
        // Method implementation
    }
}

Here, “myMethod” is a final method that has been declared in the “SuperClass” class. It is marked with the “final” keyword, which means that it cannot be overridden by any subclass, including the “SubClass” class.

Final methods are typically used when a method in a superclass should not be changed by subclasses. This can help to ensure that the behavior of the superclass is consistent across all subclasses.

Here’s an example of how a final method can be used in Java:

public class Shape {
    public final double getArea() {
        // Method implementation
    }
}

public class Circle extends Shape {
    // This will result in a compile-time error
    public double getArea() {
        // Method implementation
    }
}

In this example, the “Shape” class has a final method called “getArea” that returns the area of the shape. The “Circle” class extends the “Shape” class, but it cannot override the “getArea” method because it is marked as final. This ensures that the “getArea” method in the “Shape” class is always used to calculate the area of any shape, regardless of its subclass.

3) Java final class:

In Java, a final class is a class that cannot be subclassed.

To declare a final class, you use the “final” keyword before the class declaration. For example:

final public class MyClass {
    // Class implementation
}

public class MySubClass extends MyClass {
    // This will result in a compile-time error
    // Cannot subclass final class
}

Here, “MyClass” is a final class that has been declared with the “final” keyword. This means that it cannot be subclassed by any other class. The “MySubClass” class attempts to extend “MyClass”, but this will result in a compile-time error because “MyClass” is marked as final.

Final classes are typically used when a class should not be modified or extended. This can help to ensure that the behavior of the class is consistent across all instances, and that any changes to the class are carefully controlled and managed.

Here’s an example of how a final class can be used in Java:

final public class MathUtils {
    public static final double PI = 3.141592653589793;
    
    public static double calculateArea(double radius) {
        return PI * radius * radius;
    }
}

public class MyMathUtils extends MathUtils {
    // This is not allowed because MathUtils is a final class
}

In this example, “MathUtils” is a final class that contains some math utility methods and a final constant called “PI”. The “MyMathUtils” class attempts to extend “MathUtils”, but this is not allowed because “MathUtils” is marked as final. This ensures that the “MathUtils” class is always used to perform math calculations, regardless of its subclass.

Example of blank final variable:

A blank final variable in Java is a final variable that is not initialized at the time of declaration. Instead, it is assigned a value only once, either in the constructor of the class or in an instance initializer block.

Here’s an example of a blank final variable:

public class MyClass {
    final int x;
    
    public MyClass(int x) {
        this.x = x;
    }
}

In this example, “x” is a blank final variable. It is declared as final, which means that it can only be assigned a value once. However, it is not initialized at the time of declaration. Instead, it is initialized in the constructor of the class, using the “this” keyword to refer to the instance variable.

public class MyClass {
    final int x;
    
    {
        // Instance initializer block
        x = 5;
    }
}

Alternatively, the blank final variable can also be initialized in an instance initializer block, like in this example.

In both cases, once the blank final variable has been assigned a value, it cannot be changed. This ensures that the variable is initialized only once and that its value remains consistent throughout the life of the object.

Blank final variables are useful in situations where a value needs to be assigned to a final variable at runtime, but the value cannot be determined at the time of declaration.

static blank final variable:

A static blank final variable in Java is a final variable that is not initialized at the time of declaration and is associated with the class rather than with any specific instance of the class. Instead, it is assigned a value only once, either in a static initializer block or by directly initializing it at the time of declaration.

Here’s an example of a static blank final variable:

public class MyClass {
    static final int X;
    
    static {
        // Static initializer block
        X = 10;
    }
}

In this example, “X” is a static blank final variable. It is declared as final and static, which means that it can only be assigned a value once and that it is associated with the class rather than with any specific instance of the class. However, it is not initialized at the time of declaration. Instead, it is initialized in a static initializer block, which is executed once when the class is loaded into memory.

Alternatively, the static blank final variable can also be initialized at the time of declaration, like in this example:

public class MyClass {
    static final int X = 10;
}

In both cases, once the static blank final variable has been assigned a value, it cannot be changed. This ensures that the variable is initialized only once and that its value remains consistent throughout the lifetime of the program.

Static blank final variables are useful in situations where a value needs to be assigned to a final variable at runtime, but the value cannot be determined at the time of declaration and the variable should be associated with the class rather than with any specific instance of the class.