GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 460 of 502    Print  
How to make a class immutable (like String)?

  
Total Answers and Comments: 6 Last Update: August 18, 2009     Asked by: sha_kr2001 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: sha_kr2001
 
Dear Mr Tarun,

I dont  think that u have not understood the question.Immutable means when ever u will try to change the value of a string then a new object will be created  and will point to that new object not to the original one. Using final means that u r not allowing that class to extend...

Above answer was rated as good by the following members:
shouvanik1979, swpnl_ptl, james.vinett, sujithkthomas
December 27, 2007 00:39:45   #1  
TarunV Member Since: December 2007   Contribution: 1    

RE: How to make a class immutable (like String)?
Use final modifier
 
Is this answer useful? Yes | No
December 27, 2007 01:43:51   #2  
sha_kr2001 Member Since: December 2007   Contribution: 7    

RE: How to make a class immutable (like String)?
Dear Mr Tarun

I dont think that u have not understood the question.Immutable means when ever u will try to change the value of a string then a new object will be created and will point to that new object not to the original one. Using final means that u r not allowing that class to extend...

 
Is this answer useful? Yes | NoAnswer is useful 3   Answer is not useful 0Overall Rating: +3    
January 11, 2008 09:24:09   #3  
satyd Member Since: January 2008   Contribution: 3    

RE: How to make a class immutable (like String)?
1.The Class should be final
2.The Properties of the class should be private and should not have any setter methods

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
January 11, 2008 09:31:19   #4  
satyd Member Since: January 2008   Contribution: 3    

RE: How to make a class immutable (like String)?

More drtails will be:-
A class will be immutable if all of the following are true:

1)All of its fields are final
2)The class is declared final
3)The this reference is not allowed to escape during construction
4)Any fields that contain references to mutable objects such as arrays collections or mutable classes like Date:
-Are private
-Are never returned or otherwise exposed to callers
-Are the only reference to the objects that they reference
-Do not change the state of the referenced objects after construction


 
Is this answer useful? Yes | No
February 11, 2008 06:07:13   #5  
sampra Member Since: February 2008   Contribution: 278    

RE: How to make a class immutable (like String)?
Declare class as final
 
Is this answer useful? Yes | No
August 18, 2009 03:36:36   #6  
Sarje Member Since: August 2009   Contribution: 62    

RE: How to make a class immutable (like String)?
immutable means once object is created then you can not make any change to it. if you try to do so then new object will be created and original object will be lost.
final modifier is used to indicate that the class can not be extended not to make any object immutable.
But if you make all of the instance fields final and once they are initialized through constructor they can not be changed. In this case you have to create each time a new object and your class object will be immutable.

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape