Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No, there is not. How would the compiler know what type to return? The generic type of ArrayList in the constructor will not be known during compile time. You either have to make the whole class generic or take another approach.</p> <p>Consider this:</p> <pre><code>public class Test { public static void main(String[] args) { List&lt;String&gt; arrList = new ArrayList&lt;String&gt;(); arrList.add("FOO"); Test test = new Test(arrList); String testStr = test.returnWhat(); System.out.println("testStr"); } private final List myList; //warning public &lt;T&gt; Test(List&lt;T&gt; ttype) { myList = ttype; } public &lt;T&gt; T returnWhat() { return (T) myList.get(0); //warning } } </code></pre> <p>This works but gives you warnings on the marked lines. So, really there is no way to achieve what you are describing without making the whole class generic. Because, what if:</p> <pre><code>public class Test { public static void main(String[] args) { List&lt;String&gt; arrList = new ArrayList&lt;String&gt;(); arrList.add("FOO"); Test test = new Test(); // now what? String testStr = test.returnWhat(0); // no warning... JPanel p = test.returnWhat(0); // goes through without warning, real nice... test.returnWhat(0); // returns Object Test test2 = new Test(arrList); test2.addElement(new Object()); // boom, inserted object into list of string. String nono = test2.returnWhat(1); // the universe goes down. assign an object to string without warning. even // though one COULD think the class is generic. } // private List&lt;T&gt; myList = new ArrayList&lt;T&gt;(); compiler error, T is unknown private List myList = new ArrayList(); public Test() { myList.add(new Object()); } public &lt;T&gt; Test(List&lt;T&gt; ttype) { myList = ttype; } public &lt;T&gt; T returnWhat(int index) { return (T) myList.get(index); } public &lt;T&gt; void addElement(T el) { myList.add(el); } } </code></pre> <p>The second one doesn't compile when myList is made generic. How could the compiler determine the type of &lt;T&gt; in the case where the default constructor is used? </p> <p>Further, this could lead to serious problems with Objects in collections that rely on the fact that only certain types are inserted. </p> <p>This will generate the following exception:</p> <pre><code>Exception in thread "main" java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.String at Test.main(Test.java:27) </code></pre> <p>Did I manage to convince you?</p> <p>Real nice question, btw. I had to think about this one quite a bit.</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.
 

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