This question is specifically about nested lists which is a static field of the generic class. If I am maintaining the logs in a static variable inside my class using the code:
static List<List<? extends Number>> myLog = new ArrayList<List<? extends Number>>();
myLog
is a list of list whose element is of type that extends Number
. Here, instead of extending Number
, I might want to use a generic type <? extends T>
.
I understand that type variables exist only at compile time, and all instances of a generic class has the same run-time class, due to type erasure. Then, how is the nested list (List
interface) within the top-level list (ArrayList
) is determined at compile time about its actual implementation type (ArrayList
,LinkedList
, ...)? I find it a bit complex, as a couple of things are happening at the same time. Any help is appreciated.
EDIT: So, for example, in my code I have a method sortList
which sorts a list of numbers, and it will also log that list of numbers in myLog
list, like:
public void sortList(List<? extends Number> inList) {
// do something
myLog.addLast(inList);
// do some more
}
Now, instead of using <? extends Number>
, I might want to use <? extends T>
in a generic class. So when creating the static field myLog
, when type erasure happens, how does compiler determine the type of the nested list. I hope I am a bit clearer this time.
>();` ?
– Asoub Oct 14 '16 at 07:55