The following code works:
class Factory<T> {
private Class<T> c;
public Factory( Class<T> c ) {
this.c = c;
}
public T giveNew() throws Exception {
return c.newInstance();
}
}
Assume you have a class
class Product {
public String name = "mp3-player";
}
you can do:
Factory<Product> myFactory =
new Factory<Product>( Product.class );
System.out.println(
"Name: " + myFactory.giveNew().name );
will return "Name: mp3-player" |