No, Generics is only at compile time, only to check the type of compilation, so that you avoid casts that can be checked safely at compile time.
Think about it. If you could do what you want, it would be completely useless. For example, when using ArrayList<String> means that when you get an element from it, the compiler can infer at compile time that it is of type String , and therefore it allows you to assign it to type String in code without translation. In addition, if you try to add an element to it, the compiler can check at compile time that it is of type String , or it does not allow you to do this.
But you need a type parameter that is unknown at compile time. Thus, when you get an element from it, the compiler knows nothing about its type, so you can only assign it to an Object type. And when you try to insert something, the compiler does not know what type it should allow, so should it allow all types? Then there are no generics.
Thus, you should simply use the upper bound type as the type parameter. Like ArrayList<Object> list = new ArrayList<Object>();
source share