Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

immutable object

 
Rajeev Ravindran
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
is it possible to make an Object immutable ?? for eg. consider an Object
Object obj = something();
if i call obj.toString() does it make this object immutable ?? becoz toString() returns String and String is immutable right ?? Plz guide me if im wrong..
is there any other way to make an Object immutable ??
thanks,
raj
 
Susilo Saja
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajeev,
I only have little understanding about your question, hope this help.
Immutable object is object that can't be modified. You can achieve this by making all fields private and make no method that can modify the fields. The fields is only set during the creation of the object.
Susilo
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote an article on Mutable and Immutable objects for the April newsletter. You could start there
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic