<?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: Class as a generic type</title>
	<atom:link href="http://javablog.co.uk/2007/04/27/class-as-a-generic-type/feed/" rel="self" type="application/rss+xml" />
	<link>http://javablog.co.uk/2007/04/27/class-as-a-generic-type/</link>
	<description>by Java coders, for Java coders</description>
	<lastBuildDate>Thu, 22 Jul 2010 12:24:38 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: links for 2007-05-03 &#124; Urubatan`s Weblog</title>
		<link>http://javablog.co.uk/2007/04/27/class-as-a-generic-type/comment-page-1/#comment-21</link>
		<dc:creator>links for 2007-05-03 &#124; Urubatan`s Weblog</dc:creator>
		<pubDate>Thu, 03 May 2007 10:21:54 +0000</pubDate>
		<guid isPermaLink="false">http://javablog.co.uk/2007/04/27/class-as-a-generic-type/#comment-21</guid>
		<description>&lt;p&gt;[...] Javablog » Class as a generic type   Popularity: 1% [...]&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>[&#8230;] Javablog » Class as a generic type   Popularity: 1% [&#8230;]</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Miller</title>
		<link>http://javablog.co.uk/2007/04/27/class-as-a-generic-type/comment-page-1/#comment-20</link>
		<dc:creator>Alex Miller</dc:creator>
		<pubDate>Wed, 02 May 2007 14:39:11 +0000</pubDate>
		<guid isPermaLink="false">http://javablog.co.uk/2007/04/27/class-as-a-generic-type/#comment-20</guid>
		<description>&lt;p&gt;Regarding Class and its generic type, I find it most helpful to think of Class&lt;T&gt; as a factory for instances of T (via newInstance() or even getConstructor()).&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Regarding Class and its generic type, I find it most helpful to think of Class&lt;T&gt; as a factory for instances of T (via newInstance() or even getConstructor()).</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Tim Vernum</title>
		<link>http://javablog.co.uk/2007/04/27/class-as-a-generic-type/comment-page-1/#comment-16</link>
		<dc:creator>Tim Vernum</dc:creator>
		<pubDate>Mon, 30 Apr 2007 05:32:03 +0000</pubDate>
		<guid isPermaLink="false">http://javablog.co.uk/2007/04/27/class-as-a-generic-type/#comment-16</guid>
		<description>&lt;p&gt;Yes, you are correct, I got my comment backwards.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Yes, you are correct, I got my comment backwards.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Sam</title>
		<link>http://javablog.co.uk/2007/04/27/class-as-a-generic-type/comment-page-1/#comment-15</link>
		<dc:creator>Sam</dc:creator>
		<pubDate>Sat, 28 Apr 2007 16:56:34 +0000</pubDate>
		<guid isPermaLink="false">http://javablog.co.uk/2007/04/27/class-as-a-generic-type/#comment-15</guid>
		<description>&lt;p&gt;Thanks Tim and Ricky, I think that covers the bad use example just fine. :-)&lt;/p&gt;

&lt;p&gt;I had forgotten that extends in generic types would mean that &lt;code&gt;List&lt;? extends Object&gt;.add()&lt;/code&gt; can&#039;t take &lt;code&gt;String&lt;/code&gt; parameters anymore, so in fact Ricky&#039;s correction (setting the generic type of &lt;code&gt;Arrays.asList&lt;/code&gt;) is a much better solution. (Tim, I think you meant to say &lt;code&gt;List&lt;String&gt;&lt;/code&gt; can&#039;t take the &lt;code&gt;Boolean.TRUE&lt;/code&gt;, not &lt;code&gt;List&lt;Object&gt;&lt;/code&gt;)&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Thanks Tim and Ricky, I think that covers the bad use example just fine. <img src='http://javablog.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>

<p>I had forgotten that extends in generic types would mean that <code>List&lt;? extends Object&gt;.add()</code> can&#8217;t take <code>String</code> parameters anymore, so in fact Ricky&#8217;s correction (setting the generic type of <code>Arrays.asList</code>) is a much better solution. (Tim, I think you meant to say <code>List&lt;String&gt;</code> can&#8217;t take the <code>Boolean.TRUE</code>, not <code>List&lt;Object&gt;</code>)</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Tim Vernum</title>
		<link>http://javablog.co.uk/2007/04/27/class-as-a-generic-type/comment-page-1/#comment-14</link>
		<dc:creator>Tim Vernum</dc:creator>
		<pubDate>Sat, 28 Apr 2007 15:04:21 +0000</pubDate>
		<guid isPermaLink="false">http://javablog.co.uk/2007/04/27/class-as-a-generic-type/#comment-14</guid>
		<description>&lt;p&gt;The case you are looking for is (almost) exactly the one you have shown.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# lang java
List&lt;String&gt; strList = new ArrayList&lt;String&gt;() // Valid
List&lt;Object&gt; objList = strList; // NOT valid 
objList.add(&quot;Bob&quot;); // This would be safe
objList.add(Boolean.TRUE); // This is OK for a string list,
                           // but not for an object list
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That last line if the reason why the 2nd line is not allowed.
And that&#039;s the same reason why your examples aren&#039;t allowed.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>The case you are looking for is (almost) exactly the one you have shown.</p>

<pre class="highlighted"><code><span class="hl-identifier">List</span><span class="hl-code">&lt;</span><span class="hl-identifier">String</span><span class="hl-code">&gt; </span><span class="hl-identifier">strList</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">ArrayList</span><span class="hl-code">&lt;</span><span class="hl-identifier">String</span><span class="hl-code">&gt;</span><span class="hl-brackets">()</span><span class="hl-code"> </span><span class="hl-comment">// Valid</span><span class="hl-code">
</span><span class="hl-identifier">List</span><span class="hl-code">&lt;</span><span class="hl-identifier">Object</span><span class="hl-code">&gt; </span><span class="hl-identifier">objList</span><span class="hl-code"> = </span><span class="hl-identifier">strList</span><span class="hl-code">; </span><span class="hl-comment">// NOT valid </span><span class="hl-code">
</span><span class="hl-identifier">objList</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Bob</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// This would be safe</span><span class="hl-code">
</span><span class="hl-identifier">objList</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-identifier">Boolean</span><span class="hl-code">.</span><span class="hl-identifier">TRUE</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// This is OK for a string list,</span><span class="hl-code">
                           </span><span class="hl-comment">// but not for an object list</span></code></pre>

<p>That last line if the reason why the 2nd line is not allowed.
And that&#8217;s the same reason why your examples aren&#8217;t allowed.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Ricky Clarkson</title>
		<link>http://javablog.co.uk/2007/04/27/class-as-a-generic-type/comment-page-1/#comment-12</link>
		<dc:creator>Ricky Clarkson</dc:creator>
		<pubDate>Sat, 28 Apr 2007 14:40:08 +0000</pubDate>
		<guid isPermaLink="false">http://javablog.co.uk/2007/04/27/class-as-a-generic-type/#comment-12</guid>
		<description>&lt;pre&gt;&lt;code&gt;# lang java
List&lt;Object&gt; stooges = Arrays.&lt;Object&gt;asList(&quot;Larry&quot;, &quot;Moe&quot;, &quot;Curly&quot;);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;p&gt;&lt;p&gt;&lt;p&gt;Set your IDE to show use of a &#039;raw type&#039; as a warning/error.&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&quot;Does anyone know of a valid case where it would be bad to allow a sub-type to be assigned in place of a type?&quot;&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# lang java
List&lt;Number&gt; numbers=new ArrayList&lt;Double&gt;(); //not allowed, but imagine
List&lt;Double&gt; doubles=numbers;
numbers.add(5);
double d=doubles.get(0); //ClassCastException, we don&#039;t want this.
&lt;/code&gt;&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<pre class="highlighted"><code><span class="hl-identifier">List</span><span class="hl-code">&lt;</span><span class="hl-identifier">Object</span><span class="hl-code">&gt; </span><span class="hl-identifier">stooges</span><span class="hl-code"> = </span><span class="hl-identifier">Arrays</span><span class="hl-code">.&lt;</span><span class="hl-identifier">Object</span><span class="hl-code">&gt;</span><span class="hl-identifier">asList</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Larry</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">Moe</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">Curly</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;</span></code></pre>

<p></p><p></p><p></p><p>Set your IDE to show use of a &#8216;raw type&#8217; as a warning/error.</p>

<p><em>&#8220;Does anyone know of a valid case where it would be bad to allow a sub-type to be assigned in place of a type?&#8221;</em></p>

<pre class="highlighted"><code><span class="hl-identifier">List</span><span class="hl-code">&lt;</span><span class="hl-identifier">Number</span><span class="hl-code">&gt; </span><span class="hl-identifier">numbers</span><span class="hl-code">=</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">ArrayList</span><span class="hl-code">&lt;</span><span class="hl-identifier">Double</span><span class="hl-code">&gt;</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-comment">//not allowed, but imagine</span><span class="hl-code">
</span><span class="hl-identifier">List</span><span class="hl-code">&lt;</span><span class="hl-identifier">Double</span><span class="hl-code">&gt; </span><span class="hl-identifier">doubles</span><span class="hl-code">=</span><span class="hl-identifier">numbers</span><span class="hl-code">;
</span><span class="hl-identifier">numbers</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-number">5</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">d</span><span class="hl-code">=</span><span class="hl-identifier">doubles</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//ClassCastException, we don't want this.</span></code></pre>]]></content:encoded>
	</item>
</channel>
</rss>
