Instantiate generic type in java

I'm stuck. Coming from C++ I thought this would simply work, but it does not. Can you please give me an advice? I will try to not end up with kind of creation method in each class used for T.

public class A<T>{

    private T t_;

    public A(){
        t_ = new T(); //error
    }

}  

Also i don't want to have constructor looking like: A(Class classT){ ... Ideally i would like to have something like this c++ code.

template<class T>
class A{
private:
   T t_;

public:
   A(){}

};

Thanks for help.

Answer:1

You cannot do such things in Java. Read about type erasure: http://docs.oracle.com/javase/tutorial/java/generics/erasure.html

Answer:2

Besides the issue of type erasure -- which makes this impossible in any event -- you simply can't guarantee that T has an exposed, no-argument constructor!

The traditional workaround is to pass around factory objects that provide methods that can be used to obtain T objects. A simplistic example would be

interface Factory<T> {
  T create();
}

and then your class can get a Factory<T> and call create() on it to get more Ts. More sophisticated examples might require additional arguments to a create method, for example, or overloads.

Less-pretty workarounds include explicit reflection, passing around Class<T> objects. For example, ArrayList.class.newInstance() returns a newly constructed raw ArrayList. You have to explicitly pass around the Class object, and the reflection-based approach is slower than a normal constructor or a factory object (even though the speed of reflection has been improving lately). You also depend on the class exposing a no-arg constructor, which isn't always the case. It can be made to work, but it's never what I'd call "pleasant."

(That said, for reference, there are indications that the situation may change somewhat in Java 8 with Project Lambda and friends.)

Answer:3

Sorry to disapoint you but this is just plain impossible in java without constructors.

Java throws away the generic information at runtime (see type erasure)

Answer:4

Is there a way to set a button key event in Java so that Alt does not have to be pressed. For example, when this is used setMnemonic(KeyEvent.VK_DELETE) it is required for Alt + Delete to be pressed ...

Below is a code snippet of ArrayList, which either takes in the 'initialCapacity' provided by the user or default of 10. My question - why was 10 not a static constant ? I guess constants are used ...

If I have a regular expression, how do I return the substring that it has found? I'm sure I must be missing something obvious, but I've found various methods to confirm that that substring is ...

While I do understand some of the corner-cases of generics, I'm missing something with the following example. I have the following class 1 public class Test<T> { 2 public static void main(...