Professional Documents
Culture Documents
TCP Lec08
TCP Lec08
Lecture 8
• Instance field locates in the objects • A static field belongs to the class.
of the class. • There is one copy of each static
• Each object of the class have its filed per class, regardless of how
own values for these fields. many objects are created from it.
public class Test { Test p = Test(); public class Test { Test.field1 = “mmu”;
String field1; p.field1 = “mmu”; static String field1; System.out.println(Test.field1);
} System.out.println(p.field1); }
Source: http://tutorials.jenkov.com/java/fields.html
Instance Field
Accessing Instance Field
To access the Java field in JNI:
1. Find class reference of the object
GetObjectClass( )
p.code = 1112998
Java source code: num = env->GetIntField(obj, fid); // read the instance field
std::cout<<"code received = "<<num<<"\n";
Book.java
class Book {
private int code; std::cout<<"Please enter new code: ";
private native void read(); std::cin>>num;
env->SetIntField(obj, fid, num); // set the instance field into new value
static { }
System.loadLibrary("CBook");
}
Step 3A: Obtain the value of instance field using Get<Type>Field() functio
JNI interface pointer object reference field id
temporary storage
Step 3B: Change the value of instance field using Set<Type>Field() functi
jint num = 10; the updated value
object reference
field id
Primitive short S
data type int I
long J
float F
double D
String Ljava/lang/String;
Class Object Ljava/lang/Object;
data type int[] [I
Example of Field Signature/Descriptor
List the field signature below:
1. int ; I
2. boolean door; Z
4. float [] price; [F
Accessing Instance Field (String Type)
Java source code: Pen.java C native code: CPen.cpp
b.brand = "Sheaffer"
Step 3A: Obtain the value of static field using GetStatic<Type>Field() function
JNI interface pointer class reference field id
It returns the value of a static
jint si = env->GetStaticCharField(cls, fid); field of a class. The field to
access is specified by field ID.
temporary storage static field(size) in the Java class is char type
Box.size = ‘M’
GetStaticCharField and SetStaticCharField because the static field size is a char data type.
Accessing Static Field (Primitive Type)
#include <iostream> C++ native code: CBox.cpp
#include "Box.h"
using namespace std;
class Q3{
public native void printTest(int num);
static {
System.loadLibrary("CQ3");
}
Q3 ww = new Q3();
ww.printTest(num);
}
}
Extra Exercise (Q1)
#include "Q3.h"
#include <iostream>
using namespace std;
return;
}
Extra Exercise (Q1)
Extra Exercise (Q2)
import java.util.Scanner;
class Q4 {
public native int printResult(int num);
static {
System.loadLibrary("CQ4");
}
Q4 ww = new Q4();
System.out.printf("The result is " + ww.printResult(num));
}
}
Extra Exercise (Q2)
#include "Q4.h"
jint temp;
return temp;
}
Extra Exercise (Q2)
Extra Exercise (Q1)
import java.util.Scanner;
class Q5{
public native void printLength(String name);
static {
System.loadLibrary("CQ5");
}
Q5 ww = new Q5();
ww.printLength(name);
}
}
Extra Exercise (Q1)
#include "Q5.h"
#include <iostream>
using namespace std;
env->ReleaseStringUTFChars(text, str);
return;
}
Extra Exercise (Q1)