Originally posted by Olivier Croisier: You cannot be sure that the real type (T) will have a no-arg constructor. So you cannot write T t = new T() !
Actually, this is not the real reason why you cannot write new T(). The problem is that generics in Java is supported by a technique called type erasure. With this technique, the generic types only exist at compile time. So, the type T is not really replaced by the real type during runtime. That's why you cannot write new T().
One thing you could do to help clear out things is to compile the following code and disamssemble it (using javap -c). Note that the disassembled code is the same.
[ July 02, 2007: Message edited by: Leandro Melo ]
Originally posted by kumarth ravi: It was compiled but thrown a Class cast exception.
That's what Manfred was trying to tell you. Replacing T o=new T() by java.lang.Object o=new java.lang.Object() might not be a good idea. You have just seen a situation where it wouldn't work . You might want to take a look at the tutorial I pointed in my first post, it could help you for a better understanding (if you're still confused).