Java Final – Final Variables, Final Methods and Final Class

”final” keyboard can be used with methods, classes and variables in Java. Each use has different meaning that we are going to discuss in details in this tutorial : final variable, final method and final class.

1. Final Variable :

final variables are variables that cannot be changed i.e. constant. It is really good practice to use final variables using uppercase letters and underscores as separator. Something as like below :

final int MY_VARIABLE = 10;

MY_VARIABLE cannot be changed after this initialization. If you want to change, you will get compile time error.

Initialize a final variable in Instance initializer block , in constructor or in static block:

Final variables that are not declared at the time of declaration are called blank final variables. But wait ..how can it be a constant without holding a value ? Yes, we need to initialize them as well. Initialization can be done either in a instance initializer block, or in a constructor .  For a final variable that is static, we should initialise it inside static block. Let’s take a look into these three different scenarios. :

public class Car {
    final int PRICE;
    final String COLOR;
    final static String OWNER_NAME;

    {
        COLOR = "black";
    }

    static {
        OWNER_NAME = "Adam";
    }

    Cars(int price) {
        PRICE = price;
    }
}

In the above example, for the class “Car”, three final variables are defined. We can change the “PRICE” variable using its constructor each time a new object will create.
Final variables are used as constants. For global constants, static final variables are used.

Final methods :

Similar to final variables, we can have final method. Means method that  cannot be changed. Behaviour of a method can only be changed by overriding it in another class. So, final methods are not allowed to override. Example :
Car.java

public class Car {
    final int WHEELS = 4;

    public final int getNoOfWheels(){
        return WHEELS;
    }
}

Van.java

public class Van extends Car {


}

Main.java

public class Main {

    public static void main(String[] args){
        Van van = new Van();
        System.out.println("no of wheels of a Van "+van.getNoOfWheels());

    }


}

In the above example we have one Car class with one final method getNoOfWheels() that returns 4 . We have created one new class ‘Van’ extending ‘Car’ . In ‘Main’ class, we are able to access the final method ‘getNoOfWheels’ from ‘van’ object. i.e. it is inheriting the method from its parent class. But if we try to override it inside ‘Van’ class, one compile-time error will be thrown mentioning that a final method cannot be overriden.

Final Class :

Final class is a class that cannot be extended i.e. it cannot be inherited. e.g. Int and Float are final classes .

public final class Car {
    final int WHEELS = 4;

    public final int getNoOfWheels(){
        return WHEELS;
    }
}

Now, if we try to create another class by extending class ‘Car’, it will show one compile time error message.

Notes on final keyword in Java :

1. Final keyword can be applied to variable,method and class in Java.
2. Final variables cannot be changed, final methods cannot be override and final class cannot be extended.
3. Final variables should be initialised always. At the time of declaration, inside constructor, inside static method (for static final variables ) or inside instance initializer block.
4. A constructor cannot be final
5. All variables declared inside interface are final
6. Using final variables, methods and classes in Java improves performance.

Leave a Reply