ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

Final object in Java

Subject: 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. }
Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!