The final modifier indicates that an object is fixed and cannot be changed. When we use this modifier at a class level, it means that the class can never have subclasses. When we apply this modifier to a method, the method can never be overridden. When we apply this modifier to a variable, the value of the variable remains constant. We will get a compile-time error if we try to override a final method or class. We will also get a compile-time error if we try to change the value of a final variable.
Because private methods cannot be subclassed, they can also be treated as final. Final allows the Java compiler to make some optimization to improve the performance of the code. If the compiler knows that a method cannot be subclassed, it will certainly not look in the subclasses for the matching method when a method is called.
To declare a method as final, place the modifier as follows:
public final void hra()
A class is declared as final to ensure security.
class circle { int r; float a; final float pi=(float)22/7; void setradius(int x) { r=x; } void disparea() { a=pi*r*r; System.out.println("Area of Circle is : "+a); } } class FinalVariable { public static void main(String args[]) { circle k=new circle(); k.setradius(9); k.disparea(); } }