static int global = 0;
JNIEXPORT void JNICALL Java_com_example_YourClass_add(JNIEnv* env, jclass klass, jint value)
{
global += value;
}
JNIEXPORT jint JNICALL Java_com_example_YourClass_getSum(JNIEnv* env, jclass klass)
{
return global;
}You wrap it with this Java code:
package com.example;
class YourClass
{
static {
System.loadLibrary("...");
}
public static native void add(int value);
public static native int getSum();
}Does that clear up things? Of course, its a bit bad example, because it uses global variables. But you can always return Java class instance (jobject) that stores needed data.
Java only clear memory for things allocated through JNI. You are resposnible for everything else (fopen, new, malloc, ...)