I don't have the references in front of me right now, but there is a solution that involves passing a "dummy" arg to the constructor whose type has to be , then you can use the class of that param at runtime to instantiate your own vars with the same class. (I guess that's what's happening when you pass an empty array to ArrayList's toArray mathod.)
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Unfortunately, we do not live in a perfect world, and this doesn't work because of type erasure.
Why doesn't it work? Instantiating a type parameter won't work, but instantiating parameterized classes which accept a type argument will. The below snippet won't work, but what you posted will.
// doesn't work
public class MyGenericClass<T> {
private T item;
public MyGenericClass() {
item = new T();
}
}
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734