by Dinesh Thakur Category: Data Type

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)
                     void disparea()
                         System.out.println("Area of Circle is : "+a);
                       class FinalVariable
                           public static void main(String args[])
                                  circle k=new circle();

Final Variable in Java Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Search Content