Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first invocation is passing an instance of a anonymous subclass of <code>ArrayList&lt;Integer&gt;</code>. So, it's similar to:</p> <pre><code>class YourClass extends ArrayList&lt;Integer&gt; </code></pre> <p>and you invoking the method as:</p> <pre><code>printType(new YourClass()); </code></pre> <p>The generic superclass of <code>YourClass</code> or the anonymous class in your case is <code>ArrayList&lt;Integer&gt;</code> only. So, that output is clear.</p> <hr /> <p>As for your 2nd case, you are passing an instance of <code>ArrayList&lt;Integer&gt;</code> itself. And definition of this class looks like:</p> <pre><code>public class ArrayList&lt;E&gt; extends AbstractList&lt;E&gt; implements List&lt;E&gt;, RandomAccess, Cloneable, java.io.Serializable </code></pre> <p>So, the generic superclass here is <code>AbstractList&lt;E&gt;</code>.</p> <p><strong>Instantiation of generic type share same <code>Class</code> instance:</strong></p> <p>Note that the all the instantiation of <code>ArrayList</code> share the same class at runtime:</p> <pre><code>new ArrayList&lt;Integer&gt;().getClass() == new ArrayList&lt;String&gt;().getClass(); </code></pre> <p>The above comparison will give you <code>true</code>. Because both the <code>getClass()</code> invocation gives back to you:</p> <pre><code>class java.util.ArrayList </code></pre> <p>See <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.2" rel="nofollow">JLS 8.1.2: Generic Classes and Type Parameters</a>:</p> <blockquote> <p>A generic class declaration defines a set of parameterized types (§4.5), one for each possible invocation of the type parameter section by type arguments. All of these parameterized types share the same class at run time.</p> <p>For instance, executing the code:</p> <pre><code> Vector&lt;String&gt; x = new Vector&lt;String&gt;(); Vector&lt;Integer&gt; y = new Vector&lt;Integer&gt;(); boolean b = x.getClass() == y.getClass(); </code></pre> <p>will result in the variable <code>b</code> holding the value <code>true</code>.</p> </blockquote> <p>In other words, <code>getGenericSuperClass()</code> method will not give you the actual type argument you use while instantiating your class, but the type parameter used in the class it is extending. Now, since the generic superclass of <code>java.util.ArrayList</code> is <code>AbstractList&lt;E&gt;</code>. And hence the type parameter you get will be <code>E</code> only. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload