Final Keyword in Java

     final keyword is used in three different contexts which are:

  • variable
  • method
  • class

Let's discuss about them in detail:
1. final variable:
            When we declare a variable with final keyword, then we cannot modify the value of the variable further. Basically, we use final to declare variable with a constant value.
            Example:
                    class Operation{
                        public final double PI = 3.14;
                        
                         public void myMethod(){
                                PI = 1.618 ; //this line will throw an error, because final variable cannot be modified
                        }
                    }

            Since we cannot modify the value of final variable further, which means that we have to initialize the value of final variable while declaring it.
            Hence there are two ways to declare a final variable:
            a. While declaring the final variable:
                        public final double PI = 3.14;

            b. Using constructor or static block:
                        - using constructor:
                                    class Operation{
                                            public final double PI;
                                            public Operation(){
                                                    PI = 3.14;
                                            }
                                    } 

                        - using static block
                                    class Operation{
                                            public final double PI;
                                            static{
                                                PI = 3.14;
                                            }
                
                                            public Operation(){
                                                System.out.println("Constructor called..");
                                            }
                                    }

               Note: If final variable is not declared using above ways, then an error will occur.

2. final method:
                When a method is declared with final keyword, it is said to be a final method.
                Note: A final method cannot be overridden.

                Example:
                          Case 1:
                                class Operation{    
                                         final int add(int a, int b){
                                                return a+b;
                                        }
                                }
    
                                class Calculator extends Operation{
                                        public int add(int a, int b){
                                               //since add method is final, hence overriding it will throw a compilation error
                                        }

                                }

                          Case 2:
                                class Operation{    
                                         final int add(int a, int b){
                                                return a+b;
                                        }
                                }
    
                                class Calculator extends Operation{
                                        public int add(ArrayList<Integer> numbers){
                                              int sum = 0;
                                              for(int i: numbers){
                                                    sum+=i;
                                                }
                                              return sum;
                                        }

                                }

                        Case 2 example will not throw any compilation error, because for method to be overridden, signature of method must be same. But in case 2 signature of add(int a, int b) of class Operation is different than than of add(ArrayList<Integer> numbers).

To understand the concept of Overriding and Overloading: click here

3. final class:
                When a class is declared with final keyword, it is said to be a final class.
                Note: A final class cannot be inherited i.e. extended.
                Example:
                        final class Operation{
                                //statements
                        }
            
                         //Since we cannot extend a final class, below code will throw a compilation error  
                        class Calculator extends Operation{
                               //statements
                        }








Comments

Popular Posts