Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I instantiate a Java generic with JNI?

If I want to instantiate a Date, I can use:

jclass cls = (*env)->FindClass(env, "java/util/Date");
jmethodID ctr = (*env)->GetMethodID(env, cls, "<init>", "()V");
jobject obj = (*env)->NewObject(env, cls, ctr);

But how do I instantiate an ArrayList<String>?

like image 205
rid Avatar asked May 01 '11 19:05

rid


People also ask

Can you instantiate a generic type in Java?

Generic types are instantiated to form parameterized types by providing actual type arguments that replace the formal type parameters. A class like LinkedList<E> is a generic type, that has a type parameter E .

Can you instantiate a generic type?

A generic type is like a template. You cannot create instances of it unless you specify real types for its generic type parameters. To do this at run time, using reflection, requires the MakeGenericType method.

Can a generic class be instantiated without specifying an actual type argument?

You can create an instance of a generic class without specifying the actual type argument. An object created in this manner is said to be of a raw type. The Object type is used for unspecified types in raw types.


1 Answers

In the same way. On VM level, there are no generics.

like image 166
Christoph Walesch Avatar answered Oct 13 '22 16:10

Christoph Walesch