there is such a class

public class MyClass<T extends Entity> {} 

How can I instantiate an instance of such a class using Class.forName or in some other way if I initially know the name MyClass and the name of the class T ?

  • Help is an example - Grundy
  • Can you give an example of the code you are talking about in response? - Kirill Stoianov
  • no, it's better to add a little more information to the question first. Maybe I didn’t quite understand what exactly you have at the entrance and what exactly you want to end up with - Grundy
  • I added, as a result, I want to get a copy of the class with a generic type of type I need - Kirill Stoianov

1 answer 1

Creating an instance of a parameter in java prohibited due to the fact that generics in java implemented using the type erasure mechanism. Because of this implementation, at run time, the parameter type is replaced with Object or the type specified as the parameter bounding type. That is, if the class MyClass<T extends Entity> specified, then after compilation T is replaced with Entity everywhere, and where a typed object is needed, an explicit type conversion is used: SpecificEntity ent = (SpecificEntity)entetyObj; .

Thus, in a good way, when it is necessary to create instances of a parameter object, then a generalization subtype should be created with a parameter of a specific type, for example like this:

 public class MyClass<T>{} public class MyClass2 extends MyClass<String>{ public String get() { return new String("test"); } } 

For the case if the generic type is specified with a restriction (bound), it is possible to create instances of the classes of the “boundary” itself or the heirs of this class (boundary) without the use of reflection. In the code, you need to perform an explicit type conversion to the type of the parameter, for example like this:

 public class MyClass<T extends CharSequence>{ public T get() { return (T) new String("test"); } } 

But in this case, you create code that deliberately works with errors. If you use a class with any type that is not a type that you create explicitly, you will receive a runtime error ClassCastException.

In the case of the example above: for T extends CharSequence any descendant of CharSequence that is not a String type, for example, StringBuffer , will cause a ClassCastException .

 MyClass<StringBuffer> c = new MyClass<StringBuffer>(); c.get().append(" второй test"); /*компилятор не видит проблемы и считает, что мы получаем тип StringBuffer, но во время выполнения получим ошибку: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.StringBuffer */ 

As a result, the meaning of using generalization is lost - the compiler now cannot verify the correctness of using types and warn you about an error at the compilation stage.

  • Why did you give the second example? This is an unsafe code - if someone uses StringBuffer sb = new MyClass<StringBuffer>().get(); , it will receive a ClassCastException . - Roman
  • @Roman, you are right, I will correct the answer - I will point out that this is not a safe option, and that it spoils the very idea of ​​using generalization. - Mikhailov Valentine