What is a final variable in java?


The final keyword can be used with class method and variable. A final class cannot be inherited, a final method cannot be overridden and a final variable cannot be reassigned.

A final variable can be explicitly initialized only once. A reference variable declared final can never be reassigned to refer to a different object. However, the data within the object can be changed. So, the state of the object can be changed but not the reference. With variables, the final modifier often is used with static to make the constant a class variable.

Example

public class Test {
final int value = 10;
public static final int BOXWIDTH = 6;
static final String TITLE = "Manager";

public void changeValue() {
value = 12;
}
}

Ali
Ali

Updated on: 29-Jul-2021

468 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements