nitin1 15 Master Poster

Hi,

I have been working on JNI. I have one doubt. When I call any native method from android activity (Java code), then we get jclass object from jobject instance we get in the native method as a parameter. Why do we need it actually?

Here is a snippet from one website:

JNIEXPORT void JNICALL Java_TestJNIInstanceVariable_modifyInstanceVariable
          (JNIEnv *env, jobject thisObj) {
   // Get a reference to this object's class
   jclass thisClass = (*env)->GetObjectClass(env, thisObj);

   // int
   // Get the Field ID of the instance variables "number"
   jfieldID fidNumber = (*env)->GetFieldID(env, thisClass, "number", "I");
   if (NULL == fidNumber) return;

   // Get the int given the Field ID
   jint number = (*env)->GetIntField(env, thisObj, fidNumber);
   printf("In C, the int is %d\n", number);

   // Change the variable
   number = 99;
   (*env)->SetIntField(env, thisObj, fidNumber, number);

We got jclass from getObjectclass method.

  1. We used "thisClass" for fieldID.
  2. We used thisObj while getting the variable value (int).

When should we use what? What's the role of getting jclass obj reference? Can you please clear this thing? Thanks in advance.