java - Get classname of type used with extends ArrayList<T>? -


i have following:

public class foo<t extends grok> extends arraylist<t> {  } 

how can find class name represented t @ runtime? instance, let's allocate this:

foo<apple> foo = new foo<apple>(); 

is there way can apple class name used in constructor of foo?:

public foo() {     super();      string classname = this.gettypeclassname(); // "com.me.apple" } 

i add method set classname, prefer fetch internally user doesn't have it,

thank you

marcelo's answer explains can't want, due type erasure. however, there other options doing want, this:

public class foo<t extends grok> extends arraylist<t> {   private foo(class<t> type) {     string classname = type.getname();     ...   }    public static <t extends grok> foo<t> create(class<t> type) {     return new foo<t>(type);   }    ... } 

this enables clients of class write following, approximately same effort normal constructor gives type name too.

foo<apple> foo = foo.create(apple.class); 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -