Method 1. Caching at the Point of Use
static jmethodID cid = NULL;
if (cid == NULL){
cid = (*env)->GetMethodID(env,stringClass,"<init>","([C)V");
if (cid == NULL){
return NULL;
}
}
Method 2. Caching in the Defining class's Initializer
in java,
class InstanceOfMethodCall{
private static native void initIDs();
Static{
System.loadLibrary("InstanceOfMethodCall");
initIDs();
}
}
in C,
in .c file,
jmethodID mid;
initIDs(JNIEnv *env, jclass cls){
mid = (*env)->GetMethodID(env, cls, "METHOD_NAME","()V");
}