Accessing Java fields and methods

The JNI compiler maps each Java instance field to a pair of methods in the C++ proxy class; for example, the Java field xyz is mapped to the getXyz and setXyz methods. The compiler maps final fields to a single method of the same name; field xyz is mapped to method xyz, which is a getter method only. If a final field is a primitive type, it is mapped to a C++ constant with the same name.

The following code fragments display how you can get field values using C++ proxy class methods:

jint i = mc.getMyInt();
String s1 = mc.getMyString();

// static field uses static method
String s2 = A_B_MyClass::getMyStaticString(); 

// static primitive field uses C++ constant
double pi = A_B_MyClass::PI; 

The code fragments below illustrate how you can set field values using C++ proxy class methods:

mc.setMyInt(123);
mc.setMyStringField(String("abc"));

// static field uses static method
A_B_MyClass::setMyStaticString(String("xyz")); 

To get the java.lang.Class definition for a class—which is equivalent to accessing a static field named “class” in Java—use the following syntax:

java_lang_Class myClass = A_B_MyClass::_class();