• Advertisement
Sign in to follow this  

[java] Immutable Objects

This topic is 4395 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to write a java class that would allow for immutable objects once instanced. How would I go about doing this?

Share this post


Link to post
Share on other sites
Advertisement
Can you clarify? What do you mean by "allow for immutable objects"?

If you mean that you want the object to be immutable, you just don't give it any "mutator" methods... no way to alter it.

Share this post


Link to post
Share on other sites
Well, there is no clearly defined way to make any object you instantiate immutable, you must either create an immutable subclass or if specified in documentation for that class. To create an immutable class you would make the class final. You can also make immutable methods with the final key word. Immutable in the sense that they cannot be changed in further subclasses.

[Edited by - 5MinuteGaming on April 7, 2006 1:10:34 PM]

Share this post


Link to post
Share on other sites
Immuatable doesn't mean constant or final. Strings, for example, are always immutable. You cannot make any changes to them once they are instantiated.

I guess it really depends on what the OP means by immutable, in context.

Share this post


Link to post
Share on other sites
Quote:
Original post by 5MinuteGaming
Well, to create immutable objects you would make the class final.


Not at all. The final keyword applied to a class means it cannot be subclassed.

Also, when you declare an object final, you can still change it assuming the object provides methods to do so. In your example above, declaring immutable final ensures it may not reference another object - that's all it means. Also, final objects may be aliased.

The reason you can't change a String once you instantiate it is String simply provides no methods to do so.


public class A
{
private final StringBuilder s = new StringBuilder("Hi.");

public A(String str)
{
s.append(str); // LEGAL because the objects final object handles point to may be changed
StringBuilder t = new StringBuilder("Stuff");
s = t; // ILLEGAL because s is final; legal otherwise
StringBuilder u = s; // LEGAL
}
}




[Edited by - Kevinator on April 7, 2006 12:42:02 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Kevinator
Quote:
Original post by 5MinuteGaming
Well, to create immutable objects you would make the class final.


Not at all. The final keyword applied to a class means it cannot be subclassed.

Also, when you declare an object final, you can still change it assuming the object provides methods to do so. In your example above, declaring immutable final ensures it may not reference another object - that's all it means. Also, final objects may be aliased.

The reason you can't change a String once you instantiate it is String simply provides no methods to do so.


I may not have understood the OP's question, based on unclearness of what I believed he meant by the word 'immutable'.

In this case then if the definition of immutable is that the contents of the "object" never change. I would say the only way an instantiated object is GUARANTEED to not change is if it is stated in the documentation and specifications that it is immutable as in the case of String.

You could also GUARANTEE that an object is immutable by declaring all member variables within a class to be final, given that the member variables are either themselves immutable or are primitive types.

My statement is of course as you have pointed out incorrect.

But declaring a class final still creates immutability in the sense that the class is immutable not the contents of the object.

Thus I should have stated, "to create an immutable class you would simply make the class final." That would have been corrected.

Also, additional references to a final object may be made. But as I stated above if that object's members are declared as final then the object is GUARANTEED to be immutable.

Any semantical debates we wish to get into from here Kevinator?

Share this post


Link to post
Share on other sites
Immatable and final mean two different things. Strings are immutable because the string data is constant. They are also final, meaning that you can't derive some pseudo-mutable version of it.

[Edited by - smitty1276 on April 7, 2006 9:31:46 AM]

Share this post


Link to post
Share on other sites
Sorry. It doesn't sound like I was very clear. I want to instance an object that once created the data held within the object will not be able to be changed (immutable).

Share this post


Link to post
Share on other sites
The first post is entirely accurate to do what you've described. You just don't provide any methods that change the data!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement