GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 460 of 493    Print  
How to make a class immutable (like String)?

  
Total Answers and Comments: 5 Last Update: February 11, 2008     Asked by: sha_kr2001 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
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 | No
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 | No
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: 279    

RE: How to make a class immutable (like String)?
Declare class as final
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape