NewObject
NewObjectA
NewObjectV
jobject NewObject(JNIEnv *env, jclass clazz, jmethodID methodID, ...);
jobject NewObjectA(JNIEnv *env, jclass clazz, jmethodID methodID, jvalue *args);
jobject NewObjectV(JNIEnv *env, jclass clazz, jmethodID methodID, va_list args);
Constructs a new Java object. The method ID indicates which constructor method to invoke. This ID must be obtained by calling
GetMethodID()
with <init>
as the method name and void
(V
) as the return type.
The
clazz
argument must not refer to an array class.NewObject
Programmers place all arguments that are to be passed to the constructor immediately following the
methodID
argument. NewObject()
accepts these arguments and passes them to the Java method that the programmer wishes to invoke.LINKAGE:
Index 28 in the JNIEnv interface function table.
NewObjectA
Programmers place all arguments that are to be passed to the constructor in an
args
array of jvalues
that immediately follows the methodID
argument. NewObjectA()
accepts the arguments in this array, and, in turn, passes them to the Java method that the programmer wishes to invoke.LINKAGE:
Index 30 in the JNIEnv interface function table.
NewObjectV
Programmers place all arguments that are to be passed to the constructor in an
args
argument of type va_list
that immediately follows the methodID
argument. NewObjectV()
accepts these arguments, and, in turn, passes them to the Java method that the programmer wishes to invoke.LINKAGE:
Index 29 in the JNIEnv interface function table.
PARAMETERS:
env
: the JNI interface pointer.clazz
: a Java class object.methodID
: the method ID of the constructor.Additional Parameter for NewObject:
arguments to the constructor.
Additional Parameter for NewObjectA:
args
: an array of arguments to the constructor.Additional Parameter for NewObjectV:
args
: a va_list of arguments to the constructor.RETURNS:
Returns a Java object, or
NULL
if the object cannot be constructed.THROWS:
InstantiationException
: if the class is an interface or an abstract class.OutOfMemoryError
: if the system runs out of memory.
Any exceptions thrown by the constructor.
Example - NewObjectA
{
jclass cls;
jmethodID constructor;
jvalue args[2];
jobject object;
// get a reference to your class if you don't have it already
cls = (*env)->FindClass(env, "mypackage/Student");
// get a reference to the constructor; the name is <init>
constructor = (*env)->GetMethodID(env, cls, "<init>", "(ILjava/lang/String;)V");
// set up the arguments; i means int, l means object
args[0].i = 23;
args[1].l = (*env)->NewStringUTF(env, "Jack");
object = (*env)->NewObjectA(env, cls, constructor, args);
jmethodID constructor;
jvalue args[2];
jobject object;
// get a reference to your class if you don't have it already
cls = (*env)->FindClass(env, "mypackage/Student");
// get a reference to the constructor; the name is <init>
constructor = (*env)->GetMethodID(env, cls, "<init>", "(ILjava/lang/String;)V");
// set up the arguments; i means int, l means object
args[0].i = 23;
args[1].l = (*env)->NewStringUTF(env, "Jack");
object = (*env)->NewObjectA(env, cls, constructor, args);
}