Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

immutable objects....

 
prateek sharmaa
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
can anyone please tell me that what are immutable objects in Java other than the following :
(1) String
(2) Wrapper Objects

and how can we make objects immutable??
 
Istvan Kovacs
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/tutorial/essential/concurrency/imstrat.html

(Under the hood, String isn't completely immutable, it's effectively immutable. The string's hash code is cached in an instance variable, but is not computed until it is first accessed.)
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prateek sharmaa wrote:and how can we make objects immutable??

Please SearchFirst. This question has been asked several times before.
 
prateek sharmaa
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Rob and Istvan for the reply and i am really sorry for posting the question without searching.

now can you please give me the answer of my first question that whether any other objects are there other thatn Strings and Wrapper objects that are immutable??
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.lang.Class
java.lang.Object
java.net.URI

And then I got bored

In essence, any class that does not allow its internal state (fields) to be changed is immutable. Any stateless object (like instances of java.lang.Object) are inherently immutable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic