Sunday, 12 April 2015

Final Keyword In Java


The final keyword in java is used to restrict the user. The java final keyword can be used in many context. Final can be:
  1. variable
  2. method
  3. class
The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only. We will have detailed learning of these. Let's first learn the basics of final keyword.
final keyword in java

1) Java final variable

If you make any variable as final, you cannot change the value of final variable(It will be constant).

Example of final variable

There is a final variable speedlimit, we are going to change the value of this variable, but It can't be changed because final variable once assigned a value can never be changed.
  1. class Bike9{  
  2.  final int speedlimit=90;//final variable  
  3.  void run(){  
  4.   speedlimit=400;  
  5.  }  
  6.  public static void main(String args[]){  
  7.  Bike9 obj=new  Bike9();  
  8.  obj.run();  
  9.  }  
  10. }//end of class  
Output:Compile Time Error

2) Java final method

If you make any method as final, you cannot override it.

Example of final method

  1. class Bike{  
  2.   final void run(){System.out.println("running");}  
  3. }  
  4.      
  5. class Honda extends Bike{  
  6.    void run(){System.out.println("running safely with 100kmph");}  
  7.      
  8.    public static void main(String args[]){  
  9.    Honda honda= new Honda();  
  10.    honda.run();  
  11.    }  
  12. }  
Output:Compile Time Error

3) Java final class

If you make any class as final, you cannot extend it.

Example of final class

  1. final class Bike{}  
  2.   
  3. class Honda1 extends Bike{  
  4.   void run(){System.out.println("running safely with 100kmph");}  
  5.     
  6.   public static void main(String args[]){  
  7.   Honda1 honda= new Honda();  
  8.   honda.run();  
  9.   }  
  10. }  
Output:Compile Time Error

Q) Is final method inherited?

Ans) Yes, final method is inherited but you cannot override it. For Example:
  1. class Bike{  
  2.   final void run(){System.out.println("running...");}  
  3. }  
  4. class Honda2 extends Bike{  
  5.    public static void main(String args[]){  
  6.     new Honda2().run();  
  7.    }  
  8. }  
Output:running...

Q) What is blank or uninitialized final variable?

A final variable that is not initialized at the time of declaration is known as blank final variable.
If you want to create a variable that is initialized at the time of creating object and once initialized may not be changed, it is useful. For example PAN CARD number of an employee.
It can be initialized only in constructor.

Example of blank final variable

  1. class Student{  
  2. int id;  
  3. String name;  
  4. final String PAN_CARD_NUMBER;  
  5. ...  
  6. }  

Que) Can we initialize blank final variable?

Yes, but only in constructor. For example:
  1. class Bike10{  
  2.   final int speedlimit;//blank final variable  
  3.     
  4.   Bike10(){  
  5.   speedlimit=70;  
  6.   System.out.println(speedlimit);  
  7.   }  
  8.   
  9.   public static void main(String args[]){  
  10.     new Bike10();  
  11.  }  
  12. }  
Output:70

static blank final variable

A static final variable that is not initialized at the time of declaration is known as static blank final variable. It can be initialized only in static block.

Example of static blank final variable

  1. class A{  
  2.   static final int data;//static blank final variable  
  3.   static{ data=50;}  
  4.   public static void main(String args[]){  
  5.     System.out.println(A.data);  
  6.  }  
  7. }  

Q) What is final parameter?

If you declare any parameter as final, you cannot change the value of it.
  1. class Bike11{  
  2.   int cube(final int n){  
  3.    n=n+2;//can't be changed as n is final  
  4.    n*n*n;  
  5.   }  
  6.   public static void main(String args[]){  
  7.     Bike11 b=new Bike11();  
  8.     b.cube(5);  
  9.  }  
  10. }  
Output:Compile Time Error

Q) Can we declare a constructor final?

No, because constructor is never inherited.

Instance initializer block:


Instance Initializer block is used to initialize the instance data member. It run each time when object of the class is created.
The initialization of the instance variable can be directly but there can be performed extra operations while initializing the instance variable in the instance initializer block.

Que) What is the use of instance initializer block while we can directly assign a value in instance data member? For example:

  1. class Bike{  
  2.     int speed=100;  
  3. }  

Why use instance initializer block?

Suppose I have to perform some operations while assigning value to instance data member e.g. a for loop to fill a complex array or error handling etc.


Example of instance initializer block

Let's see the simple example of instance initializer block the performs initialization.
  1. class Bike7{  
  2.     int speed;  
  3.       
  4.     Bike7(){System.out.println("speed is "+speed);}  
  5.    
  6.     {speed=100;}  
  7.        
  8.     public static void main(String args[]){  
  9.     Bike7 b1=new Bike7();  
  10.     Bike7 b2=new Bike7();  
  11.     }      
  12. }  

Output:speed is 100
       speed is 100
 
There are three places in java where you can perform operations:
  1. method
  2. constructor
  3. block


What is invoked firstly instance initializer block or constructor?

  1. class Bike8{  
  2.     int speed;  
  3.       
  4.     Bike8(){System.out.println("constructor is invoked");}  
  5.    
  6.     {System.out.println("instance initializer block invoked");}  
  7.        
  8.     public static void main(String args[]){  
  9.     Bike8 b1=new Bike8();  
  10.     Bike8 b2=new Bike8();  
  11.     }      
  12. }  

Output:instance initializer block invoked
       constructor is invoked
       instance initializer block invoked
       constructor is invoked
In the above example, it seems that instance initializer block is firstly invoked but NO. Instance intializer block is invoked at the time of object creation. The java compiler copies the instance initializer block in the constructor after the first statement super(). So firstly, constructor is invoked. Let's understand it by the figure given below:

Note: The java compiler copies the code of instance initializer block in every constructor.

instance initializer block

Rules for instance initializer block :

There are mainly three rules for the instance initializer block. They are as follows:
  1. The instance initializer block is created when instance of the class is created.
  2. The instance initializer block is invoked after the parent class constructor is invoked (i.e. after super() constructor call).
  3. The instance initializer block comes in the order in which they appear.

Program of instance initializer block that is invoked after super()

  1. class A{  
  2. A(){  
  3. System.out.println("parent class constructor invoked");  
  4. }  
  5. }  
  6. class B2 extends A{  
  7. B2(){  
  8. super();  
  9. System.out.println("child class constructor invoked");  
  10. }  
  11.   
  12. {System.out.println("instance initializer block is invoked");}  
  13.   
  14. public static void main(String args[]){  
  15. B2 b=new B2();  
  16. }  
  17. }  

Output:parent class constructor invoked
       instance initializer block is invoked
       child class constructor invoked

Another example of instance block

  1. class A{  
  2. A(){  
  3. System.out.println("parent class constructor invoked");  
  4. }  
  5. }  
  6.   
  7. class B3 extends A{  
  8. B3(){  
  9. super();  
  10. System.out.println("child class constructor invoked");  
  11. }  
  12.   
  13. B3(int a){  
  14. super();  
  15. System.out.println("child class constructor invoked "+a);  
  16. }  
  17.   
  18. {System.out.println("instance initializer block is invoked");}  
  19.   
  20. public static void main(String args[]){  
  21. B3 b1=new B3();  
  22. B3 b2=new B3(10);  
  23. }  
  24. }  

Output:parent class constructor invoked
       instance initializer block is invoked
       child class constructor invoked
       parent class constructor invoked
       instance initializer block is invoked
       child class constructor invoked 10