Implementing private data

If the C++ class implementing your native methods requires access to C++ instance data, perform the following:

  1. Add a private long type variable called “data” to your Java class:

    public class MyClass
    {
       static
       {
          com.sybase.jni.SystemUtil.loadLibrary("MyClass");
       }
    
       private long data;
    
       public native void myMethod();
    }
    
  2. When you run jnicc, it now generates the C++ source and header files A_B_MyClassPrivateData.cpp and A_B_MyClassPrivateData.hpp.Add all private instance data to the A_B_MyClassPrivateData class. You can add any methods you want to this class.

  3. Add a public finalize method that calls a private destroy method. The JNI compiler ensures that when the destroy method is called, the C++ instance data is deleted.

    public class MyClass
    {
       static
       {
          com.sybase.jni.SystemUtil.loadLibrary("MyClass");
       }
    
       private long data;
    
       public native void myMethod();
    
       public void finalize()
       {
          destroy();
       }
    
       private native void destroy();
    }
    
  4. Access the instance data from your native methods using the data variable—see the sample in samples/SimpleRowSet.