Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

global reference to jclass

843829Dec 23 2002 — edited Dec 23 2002
Hello,

In my c++ program i made a java object with NewObject and created a global reference to it. Every time i want to call a method of that object i must call findClass (because Call<xxx>Method needs a jclass refereence).
i'm trying to create a global reference to a jclass so i don't have to call FindClass("myClass") every time, but it's impossible to create a global reference with NewGlobalRef().

Nevertheless in the following code snippet from the jni tutorial @ sun it's done this way:

/* This code is correct. */
static jclass cls = 0;
static jfieldID fld;

JNIEXPORT void JNICALL
Java_FieldAccess_accessFields(JNIEnv *env, jobject obj)
{
...
if (cls == 0) {
jclass cls1 = (*env)->GetObjectClass(env, obj);
if (cls1 == 0) {
... /* error */
}
cls = (*env)->NewGlobalRef(env, cls1);
if (cls == 0) {
... /* error */
}
fid = (*env)->GetStaticFieldID(env, cls, "si", "I");
}
/* access the member variable using cls and fid */
...
}

Is it still possible in the 1_4 version to create a global reference to a jclass ? If so, How ?

Thanx Pieter.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 20 2003
Added on Dec 23 2002
1 comment
407 views