简体   繁体   中英

associating jobject with a value

jclass object = (*env)->FindClass(env,"java/lang/Integer") // C Code

Is there any way i can associate an integer value with object ? I want object to contain/point to an integer number.

Make sure you read an understand Confusing jclass with jobject in the Pitfalls section of the JNI guide.

FindClass(env, "Foo") returns (a handle to) an object of type java.lang.Class . It is conceptually equivalent to the Class.forName(String) static method: it does not return an instance of the class you give it as a parameter ( Foo ). It returns an object of type Class which represents that class.

What you can do with a jclass (or a Class ) is find the constructor you want , and invoke that constructor to create an object of type Foo .

The JNI guide has an example of how you do this in the Invoking constructors for class String . Doing it for class Integer is similar, except for the method signature.

You'd do something like:

jclass clazz = (*env)->FindClass(env, "java/lang/Integer");
jmethodID mid = (*env)->GetMethodID(env, clazz, "<init>", "(I)V");
jobject mint = (*env)->NewObject(env, clazz, mid, 42); // your desired value here

(Requires error checking.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM