Java Reference
In-Depth Information
private int[][] array2d;
private String some
-
string;
}
The native implementation of
doStatic()
begins
JNIEXPORT void JNICALL
Java
-
javatech
-
jni22
-
JNIDemo
-
doStatic (JNIEnv *jenv,
jclass cls)
{
...
}
Since the
jclass
is received in the parameter list, it is already available for any
calls to
GetStaticFieldID()
or
GetFieldID()
.
However, the native implementation of the
doNonStatic()
method receives
a
jobject
parameter instead of a
jclass
. The reason should be obvious. Static
methods are class methods. They are available even if the class has never been
instantiated. Said another way, static methods are independent of any particu-
lar instantiation (i.e. object) of the class in which they appear. They have no
this
variable. When a static method is called, it is called without any object
of that class necessarily having been instantiated. But instance methods (i.e.
non-static methods)
require
an instantiation of the class (i.e. an object). For
this reason, instance methods are sometimes called
object
methods. Each object
method is associated with a particular object, and object methods always have an
implied
this
variable. So, while a native implementation of a static method
receives a reference to the class in which the native method is declared on
the Java side (the
jclass
parameter), the native implementation of an object
method receives a reference to the object from which it was called (the
jobject
parameter).
Since a
jclass
reference is needed to call
GetFieldID()
, receiving the
jclass
directly in the argument list of the native function that implements a static
native method is convenient for finding field IDs. Non-static native methods are
not so lucky. Obviously a
jobject
is closely related to a
jclass
(the former
is an instantiation of the latter), so it should be easy to find the
jclass
that is
associated with the
jobject
.Infact, it is easy. For a
jobject
known as
jo
,
the JNI function to get the corresponding
jclass
is
jclass cls = jenv->GetObjectClass (jo);
Using this JNI library function, non-static native implementations can find their
jclass
to use to find field IDs.
It is even possible to find field IDs for classes for which we have neither a
jclass
nor a
jobject
reference. We later discuss the
FindClass()
function
that provides this service.
Search WWH ::
Custom Search