Instantiating object of type parameter?

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 { private final Constructor ctor; private T field; MyClass(Class impl) { this. Ctor = impl.getConstructor(); } public void myMethod() throws Exception { field = ctor.newInstance(); } }.

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 { private final Constructor ctor; private T field; MyClass(Class impl) { this.

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

This may be more heavyweight than what you're looking for, but it will also work. Note that if you take this approach, it would make more sense to inject the factory into MyClass when it is constructed instead of passing it into your method each time it is called. Interface MyFactory { T newObject(); } class MyClass { T field; public void myMethod(MyFactory factory) { field = factory.newObject() } }.

Good, non-reflective approach; reflection isn't always an option. MyMethod should be able to accept a MyFactory, right? – erickson Nov 19 '08 at 0:20 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 Nov 20 '08 at 2:49.

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.

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 { 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.

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.

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

Use the new() constraint. Example: class Test where T : new() { T obj; public Test() { obj = new T(); // create a T object } }.

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

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions