Example of Final variable, Final method and Class in Java


Final in java is very important keyword and can be applied to class, method, and variables in Java. In this java final tutorial we will see what is final keyword in Java, what does it mean by making final variable, final method and final class in java and what are primary benefits of using final keywords in Java and finally some examples of final in Java. Final is often used along with static keyword in Java to make static final constant and you will see how final in Java can increase performance of Java application.


Final keyword in java can also be applied to methods. A java method with final keyword is called final method and it can not be overridden in sub-class. You should make a method final in java if you think it’s complete and its behavior should remain constant in sub-classes. Final methods are faster than non-final methods because they are not required to be resolved during run-time and they are bonded on compile time.

Benefits of final keyword in Java

Here are few benefits or advantage of using final keyword in Java:


1. Final keyword can be applied to member variable, local variable, method or class in Java.

2. Final member variable must be initialized at the time of declaration or inside constructor, failure to do so will result in compilation error.

3. You can not reassign value to final variable in Java.

4. Local final variable must be initializing during declaration. 

5. Only final variable is accessible inside anonymous class in Java.

6. Final method can not be overridden in Java.

7. Final class can not be inheritable in Java.

8. Final is different than finally keyword which is used on Exception handling in Java.

9. Final should not be confused with finalize() method which is declared in object class and called before an object is garbage collected by JVM.

10.All variable declared inside java interface are implicitly final.

11.Final and abstract are two opposite keyword and a final class can not be abstract in java.

12 Final methods are bonded during compile time also called static binding.

13.Final variables which is not initialized during declaration are called blank final variable and must be initialized on all constructor either explicitly or by calling this(). Failure to do so compiler will complain as "final variable (name) might not be initialized".

14. Making a class, method or variable final in Java helps to improve performance because JVM gets an opportunity to make assumption and optimization.

15. As per Java code convention final variables are treated as constant and written in all Caps

16.Final keyword improves performance. Not just JVM can cache final variable but also application can cache frequently use final variables.

17. Final variables are safe to share in multi-threading environment without additional synchronization overhead.

18. Final keyword allows JVM to optimized method, variable or class.

Final keyword declaration


 final int hoursInDay=24;



Program : FinalExample.java

public class FinalExample {

        public static void main(String[] args) {
             
                /*
                 * Final variables can be declared using final keyword.
                 * Once created and initialized, its value can not be changed.
                 */
                final int hoursInDay=24;
             
                //This statement will not compile. Value can't be changed.
                //hoursInDay=12;
             
                System.out.println("Hours in 5 days = " + hoursInDay * 5);
             
        }
}
Output 



G:\>javac FinalExample.java
G:\>java FinalExample
Hours in 5 days = 120


Example of Final variable, Final method and Class in Java

0 comments:

Post a Comment

 

learn java programming Copyright © 2011-2012 | Powered by appsackel.org