Listing 3: Sample native code that uses the JNI encapsulation framework (excerpt from jni_example.cpp)

JNIEXPORT void JNICALL 
Java_JniExample_native_1call 
   (JNIEnv *env, jclass clazz, jobject obj) {
  
   // Lookup the Java fields in 'obj'
   JNIField<jint> intField(env, obj, "intField");
   JNIStringUTFChars str(env, "JniExample", "stringField");
   JNIArray<jint> arr(env, obj, "intArray");

   // Set new values 
   intField = 0;
   arr[0] = 0; arr[1] = 0;
   JNIStaticField<jstring>(env, obj, "stringField") = 
      env->NewStringUTF("Good-bye, world!");

  // Destructors for 'arr' and 'str' are invoked automatically
}
— End of Listing —