Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Generic Typing in Java

Tags:

If I have a class using generic type such as

public class Record<T> {     private T value;      public Record(T value) {         this.value = value;     } } 

it is pretty straight forward to type everything during design time, if I know all types that are used such as it is the case in this example:

// I type explicitly String myStr = "A"; Integer myInt = 1; ArrayList myList = new ArrayList();  Record rec1 = new Record<String>(myStr); Record rec2 = new Record<Integer>(myInt); Record rec3 = new Record<ArrayList>(myList); 

What happens if I get a list of objects from "somewhere" where I don't know the type? How do I assign the type:

// now let's assume that my values come from a list where I only know during runtime what type they have  ArrayList<Object> myObjectList = new ArrayList<Object>();     myObjectList.add(myStr);     myObjectList.add(myInt);     myObjectList.add(myList);      Object object = myObjectList.get(0);      // this fails - how do I do that?     new Record<object.getClass()>(object); 
like image 451
Jack Gibson Avatar asked Sep 08 '11 00:09

Jack Gibson


People also ask

Can dynamic type be used for generic?

Not really. You need to use reflection, basically. Generics are really aimed at static typing rather than types only known at execution time. To use reflection, you'd use Type.

What is a dynamic type in Java?

Java is statically-typed, so it expects its variables to be declared before they can be assigned values. Groovy is dynamically-typed and determines its variables' data types based on their values, so this line is not required.

What is generic data type in Java?

Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types.

What is generic type casting in Java?

The Java Generics programming is introduced in J2SE 5 to deal with type-safe objects. It makes the code stable by detecting the bugs at compile time. Before generics, we can store any type of objects in the collection, i.e., non-generic. Now generics force the java programmer to store a specific type of objects.

What is generics in Java?

Generics means parameterized types. The idea is to allow type (Integer, String, … etc, and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types. An entity such as class, interface, or method that operates on a parameterized type is called ...

What is the key of a generic type?

The key is the type (keyvalue<,>) bit which looks weird at best. It works however and produces a non-generic type reference. You can see the difference between the full generic type and the non-typed (?) generic type in the debugger:

What is the difference between static and dynamic typing in Java?

The Java language also allows some dynamic type checking of values, especially receivers of virtual or interface method calls. But even these calls require a static type for the receiver. Dynamic typing is often more flexible than static typing because it allows programs to generate or configure types based on runtime data.

Can you use a generic type with a method?

I experimented and found that using a generic type could be done with a method even though no constraint was defined The bit that puts me off is: that is ? it can't be casting anything, there is no input of type T which constrains it to match anything, actually it doesn't really say anything anywhere.


1 Answers

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.

like image 190
DwB Avatar answered Oct 01 '22 09:10

DwB