Previous | Next | Trail Map | Using the JNI to Integrate Native Code and Java Programs | Java Native Interface Programming


Accessing Java Fields

The JNI provides functions that native methods use to get and set Java fields. You can get and set both instance fields and static (class) fields. Similar to accessing Java methods, you use different JNI functions to access instance and static fields.

Our example program, FieldAccess.java, contains a class with one static integer field si and an instance string field s. It calls the native method accessFields, which prints out the value of these two fields, and then sets the fields to new values. To verify the fields have indeed changed, we print out their values again in Java after returning from the native method.

Procedure for Accessing a Java Field

To get and set Java fields from a native method, you must do the following:

Similar to calling a Java method, we factor out the cost of field lookup using a two-step process. The field ID uniquely identifies a field in a given class. Similar to method IDs, a field ID remains valid until the class from which it is derived is unloaded.

Field Signatures

Field signatures are specified following the same encoding scheme as method signatures. The general form of a field signature is:

"field type"

The field signature is the encoded symbol for the type of the field, enclosed in double quotes (""). The field symbols are the same as the argument symbols in the method signature. That is, you represent an integer field with "I", a float field with "F", a double field with "D", a boolean field with "Z", and so on.

The signature for a Java object, such as a String, begins with the letter L, followed by the fully-qualified class for the object, and terminated by a semicolon (;). Thus, you form the field signature for a String variable (c.s in FieldAccess.java) as follows:

"Ljava/lang/String;"

Arrays are indicated by a leading square bracket ([) followed by the type of the array. For example, you designate an integer array as follows:

"[I"

Refer to the table in previous section which summarizes the encoding for the Java type signatures and their matching Java types.

You can use javap with option "-s" to generate the field signatures from class files. For example, run:

javap -s -p FieldAccess
This gives you output containing:
...
static si I
s Ljava/lang/String;
...


Previous | Next | Trail Map | Using the JNI to Integrate Native Code and Java Programs | Java Native Interface Programming