The word final in Java means, “State of an entity cannot be changed”. In Java, the final modifier can be associated with class, reference variables, primitive variables and method.
“final ” keyword is used in three ways:
i) It is used to declare constants, as final int a=10;
ii) It is used to before a method; this method cannot be overridden. Private method and Final method are the same.
Class A {
final void show() {
System.out.println(“not override a method”);
}
}
Class B {
void show() //error
{
System.out.println(“Error in this method”);
}
iii) It is used to prevent inheritance, as
final class A { ——- ——- }
Class B extend A //Error { ………… }
iv) The reference variables declared as final cannot be re-instantiated with the new operator. That is, only once we can use the new operator.