Note that there are some explanatory texts on larger screens.

plurals
  1. POjava - Accssesing "nested" Generic type
    primarykey
    data
    text
    <p>I've two classes that use generic types (A, B). My question is - What is the best way to work with the first generic type (TA) from inside B ? Here is simplified example:</p> <pre><code>public class A&lt;TListItemsType&gt; { List&lt;TListItemsType&gt; list = new LinkedList&lt;TListItemsType&gt;(); public List&lt;TListItemsType&gt; getList() { return list; } } public class B&lt;TContainderType extends A&gt; { TContainderType temp = null; public B(TContainderType cont) { temp=cont; } public void DoWork() { for (TListItemsType item : temp.getList()) { System.out.println(item); } } } </code></pre> <p>I've tried the solution suggested <a href="https://stackoverflow.com/questions/13229979/java-nested-generic-type-mismatch">here</a> -</p> <pre><code>public class B&lt;TContainderType extends A&lt;TListItemsType&gt;&gt; { TContainderType temp = null; public B(TContainderType cont) { temp=cont; } public void DoWork() { for (TListItemsType item : temp.getList()) { System.out.println(item); } } } </code></pre> <p>And it works as long as use pre defined type such as Integer or String, sadly, this doesn't work as the compiler doesn't recognize the generic as class name.</p> <p>So went further on and tried to configure another generic type and then use it inside the extends:</p> <pre><code>public class B&lt;TListItemsType, TContainderType extends A&lt;TListItemsType&gt;&gt; { TContainderType temp = null; public B(TContainderType cont) { temp=cont; } public void DoWork() { for (TListItemsType item : temp.getList()) { System.out.println(item); } } } </code></pre> <p>And it actually works, but it doesn't smell right. Is there another way to use generic type used n another generic type?</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.
 

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