Final object in Java

0
7

This tutorial is all about Final Object in Java. We cannot reinitialized Final Objects in Java. For Example: See the below code

  1. class P{
  2.  
  3. int i ;
  4.  
  5. }
  6.  
  7. class Manager{
  8.  
  9. public static void main(String args[]) {
  10.  
  11. final P p1=new P();
  12.  
  13. P.i=10; //modifying i not p1 , allowed in Java
  14.  
  15. System.out.println ("compile time success");
  16.  
  17. }
  18.  
  19. }

Above code will work fine.

See another example, here you will get error

  1. class Q {
  2.  
  3. int i ;
  4.  
  5. }
  6.  
  7. class Manager {
  8.  
  9. public static void main (String args[] )
  10.  
  11. {
  12.  
  13. final Q q1=new Q();
  14.  
  15. q1=new Q(); // compile time error
  16.  
  17. }
  18.  
  19. }

 

Readers might Read also:

 

Facebook Comments
(Visited 11 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here