Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to define immutable object?

 
David Eason
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The article Understanding that parameters are passed by value and not by reference ended this way:
<blockquote>
What if you want to disallow changes to the Point object in methods such as modifyPoint ? There are two solutions for this:

  • Pass a clone of the Point object to the modifyPoint method.

  • Make the Point object immutable.


  • </blockquote>
    My question is, how do you make an object "immutable"?
 
Rikard Qvarforth
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, if you make the method signature
"final public static void modifyPoint(final Point pt, int j)"
then the method cant change the copy of the object reference. But if you have methods in the object class that changes members within the object the final key word dosent protect that, that is if you call these methods in the "modifyPoint()" method (i think )
excuse me for my bad english
hope it helps
Rille
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic