This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to define immutable object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to define immutable object?" Watch "How to define immutable object?" New topic

How to define immutable object?

David Eason

Joined: Oct 31, 2001
Posts: 14
The article Understanding that parameters are passed by value and not by reference ended this way:
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

Joined: Jul 10, 2001
Posts: 107
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
I agree. Here's the link:
subject: How to define immutable object?
It's not a secret anymore!