Dynamic Generic Typing in Java

Java generics are not C++ Templates.

Java generics are a compile time feature, not a run time feature.

Here is a link to the Java generics Tutorial.

This can never work with Java:

new Record<object.getClass()>(object);

You must either use polymorphism (say, each object implements a known interface) or RTTI (instanceof or Class.isAssignableFrom()).

You might do this:

     class Record
     {
       public Record(String blah) { ... }
       public Record(Integer blah) { ... }
       ... other constructors.
     }

or you might use the Builder pattern.

Leave a Comment

tech