views:

918

answers:

6

I have got a template class as follows:

class MyClass<T>
{
    T field;
    public void myMethod()
    {
       field = new T(); // gives compiler error
    }
}

How do I create a new instance of T in my class?

+8  A: 

You need to provide a Class object (or a constructor) to your object, and use reflection. After type erasure, all that is known about T is that it is some subclass of Object.

class MyClass<T> {

  private final Constructor<? extends T> ctor;

  private T field;

  MyClass(Class<? extends T> impl) {
    this.ctor = impl.getConstructor();
  }

  public void myMethod() throws Exception
  {
    field = ctor.newInstance();
  }

}
erickson
+1  A: 

EDIT: Sorry. I overlooked that you are using Java. The code below works in C#.


Use the new() constraint.

Example:

class Test<T> where T : new()
{ 
  T obj; 

  public Test()
  { 
    obj = new T(); // create a T object 
  } 
}
Jonas Kongslund
A: 

Can't do better than Erickson, but while looking for a solution, I found an article that might interest you: Java theory and practice: Generics gotchas.

PhiLho
A: 

If you're willing to subclass you can avoid erasure as well, check out http://www.artima.com/weblogs/viewpost.jsp?thread=208860

krosenvold
+4  A: 
Dan Hodge
Good, non-reflective approach; reflection isn't always an option. myMethod should be able to accept a MyFactory<? extends T>, right?
erickson
Good call - you'll want to put a bounded wildcard on the factory to allow objects of type T and subclasses of T to be created in myMethod().
Dan Hodge
+2  A: 

Another non-reflective approach is to use a hybrid Builder / Abstract Factory pattern.

In Effective Java, Joshua Bloch goes over the Builder pattern in detail, and advocates a generic Builder interface:

public interface Builder<T> {
  public T build();
}

Concrete builders can implement this interface, and outside classes can use the concrete builder to configure the Builder as required. The builder can be passed to MyClass as a Builder<T>.

Using this pattern, you can get new instances of T, even if T has constructor parameters or requires additional configuration. Of course, you'll need some way to pass the Builder into MyClass. If you can't pass anything into MyClass, then Builder and Abstract Factory are out.