1

At least in Java, I don't know if there is a way to be able to do this but, I am trying to find a no-args solution for being able to pass around a class type of an unknown generic value.

class MyClass<T> {
   // Trying to find something like this...
   Class<T> classValue = T.class;
   Class<T> classValue = new Class<T>();

   // Trying to avoid this, I desire a no-args constructor solution
   MyClass(Class<T> classValue) { this.classValue = classValue }
}
turbofood
  • 192
  • 9
  • 2
    No. There is no way to achieve what you want using Java generics. This is a consequence of the way generics were implemented in Java. Specifically, type erasure. – Elliott Frisch May 05 '22 at 22:51

0 Answers0