final keyword in java - 20 salient features - Final variable, method and class



Contents of page :
  • Final keyword can be applied to following in java >
  • variable,
  • method and
  • class.
  • 1) Final variable -  once initialized final memberVariable cannot be assigned a new value in java
    • final memberVariable/instanceVariable
    • final local variable
    • final static variable
  • 2) Final method - final method cannot be inherited in java.
  • 3) Final class - final class cannot be extended in java.

  • 20 salient features of final keyword in java >



Final keyword can be applied to following in java >
  • variable,
  • method and
  • class.

1) Final variable  - once initialized final memberVariable cannot be assigned a new value in java


final can be applied to following variable types in java -
  • final memberVariable/instanceVariable
  • final local variable
  • final static variable


Final memberVariable/instanceVariable of class must be initialized at time of declaration, once initialized final memberVariable cannot be assigned a new value in java.

class FinalTest {
   final int x=1; //final memberVariable/instanceVariable
}

If final memberVariable is not declared at time of declaration we will face compilation error= “The blank final field x may not have been initialized”’.

If final memberVariable is assigned a new value we will face compilation error=”The final field FinalTest.x cannot be assigned ”.

If constructor is defined then final memberVariable can be initialized in constructor but  once initialized cannot be assigned a new value.
class FinalTest {
   final int x; //final memberVariable/instanceVariable
   FinalTest() {
          x = 1; //final memberVariable can be initialized in constructor.
   }
}


Final local variable can be left uninitialized at time of declaration and can be initialized later, but once initialized cannot be assigned a new value in java.
class FinalTest {
   void method(){         
          final int x; //final variable uninitialized at time of declaration
          x=1;
   }  
}


Final static variable of class must be initialized at time of declaration or can be initialized in static block, once initialized final static variable cannot be assigned a new value.

If static block is defined then final static variable can be initialized in static block, once initialized final static variable cannot be assigned a new value in java.
class FinalTest {
   final static int x; //final static variable
   static{ //static block
         x=1;
   }
}


So, in short using final with variable means that it cannot be assigned a new value in java.

2) Final method - final method cannot be inherited in java.

Final method cannot be overridden, any attempt to do so will cause compilation error.
Runtime polymorphism is not applicable on final methods because they cannot be inherited.


So, in short using final with method means that it cannot be inherited in java.


3) Final class - final class cannot be extended in java.
Final class cannot be extended, any attempt to do so will cause compilation error.


So, in short using final with class means that it cannot be extended in java.



20 salient features of final keyword in java >

  1. Final is a keyword in java.

  1. Final keyword can be applied to variable, method and class in java.

  1. Final memberVariable/instanceVariable of class must be initialized at time of declaration, once initialized final memberVariable cannot be assigned a new value.
class FinalTest {
   final int x=1; //memberVariable/instanceVariable
}

  1. If final memberVariable is not declared at time of declaration we will face compilation error= “The blank final field x may not have been initialized”’.

  1. If final memberVariable is assigned a new value we will face compilation error=”The final field FinalTest.x cannot be assigned ”.

  1. If constructor is defined then final memberVariable can be initialized in constructor but once initialized cannot be assigned a new value.

class FinalTest {
   final int x; //memberVariable/instanceVariable
   FinalTest() {
          x = 1; //final memberVariable can be initialized in constructor.
   }
}

Best practice : We must initialize member variables of class in constructor.

  1. Final local variable can be left uninitialized at time of declaration and can be initialized later, but once initialized cannot be assigned a new value.
class FinalTest {
   void method(){         
          final int x; //uninitialized at time of declaration
          x=1;
   }  
}

  1. Final static variable of class must be initialized at time of declaration or can be initialized in static block, once initialized final static variable cannot be assigned a new value in java.

  1. Final static member variables of class are called constants in java.

variables in interface are public, static and final by default. Interface variables are also known as constants.

  1. If static block is defined then final static variable can be initialized in static block, once initialized final static variable cannot be assigned a new value in java.
class FinalTest {
   final static int x; //static variable
   static{ //static block
         x=1;
   }
}

  1. Final method cannot be overridden, any attempt to do so will cause compilation error in java.
Runtime polymorphism is not applicable on final methods because they cannot be overridden.

  1. Final method are inherited in subclasses in java.
class Superclass {
   final void superclassFinalMethod(){
          System.out.println("in Superclass final method");
   }
}
class Subclass extends Superclass{
   void subclassmethod(){
          System.out.println("in sub class method");
   }
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class FinalTest {
   public static void main(String[] args) {
          Subclass obj=new Subclass();
          obj.superclassFinalMethod();
   }
}
/*OUTPUT
in Superclass final method
*/

superclassFinalMethod method of Superclass was inherited in Subclass.


  1. Final class cannot be extended, any attempt to do so will cause compilation error in java.

  1. Constructor of a class can never be final in java - because they are not inherited neither overridable.
Only public, protected & private are permitted modifiers with constructor.
,any attempt to make constructor final will cause compilation error=”Illegal modifier for the constructor in type FinalTest; only public, protected & private are permitted” in java.

  1. There is nothing like final object in java - if final reference variable refers to object in java than it does not mean that Object is final, it simply means that reference variable cannot refer to another object in java.
  1. final list does not means that - value cannot be added to it or removed from list.
final list means reference cannot refer to any new ArrayList, any attempt to do so will cause compilation error in java.


  1. If parameter is final its value cannot be changed, any attempt to do so will cause compilation error in java.

  1. Final class can never be abstract, because final class cannot be extended and abstract class is meant for extending
any attempt to do so will cause compilation error in java.

  1. declaring variable, class or method as final improves performance, because JVM caches them. JVM caches them because -
    1. final variable cannot be changed in java,
    2. Runtime polymorphism is not applicable on final methods because they cannot be overridden in java.
    3. final classes cannot be inherited in java.

  1. As per java naming conventions final variable must be written in UPPERCASE because they are considered as constants in java.
final int VARIABLE=1;


  1. Only final local variable can be used inside an inner class defined in a method.
Though non-final member variable can be used inside an inner class defined in a method.
class MyClass{}
class MainClass {
   int memberVar=0; //member variable
   void m(){
          final int localVar=0; //final local variable
      MyClass myClass=new MyClass(){
             void m(){
                    System.out.println(localVar);
                    System.out.println(memberVar);
             }
      };
   }      
}

Using non-final local variable inside an inner class will produce compilation error =”Cannot refer to a non-final variable localVar inside an inner class defined in a different method” in java.


So, these were the 20 salient features of final keyword in java.




Summary >
In this core java tutorial we learned about final Keyword in java language.
We learned that Final keyword can be applied to variable, method and class in java, 1) Final variable -  once initialized final memberVariable cannot be assigned a new value in java
Also we used final with
  • final memberVariable/instanceVariable
  • final local variable
  • final static variable
2) Final method - final method cannot be inherited in java.
3) Final class - final class cannot be extended in java.

And 20 salient features of final keyword in java.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.






RELATED LINKS>

Keywords in java language

final keyword in java - 20 salient features

finalize method in java - 13 salient features





eEdit
Must read for you :