What is a custom object?

what is a native object, I found that java has a peer class for interacting with native objects?

+6
java terminology
source share
2 answers

Java programs can use JNI to access functions implemented in native code (something compiled for machine code). Interaction with object-oriented native code requires the java class, which transfers the method from java to an instance of its own class using jni. This class is the java node of the native class.

Example: We have a print_hello class in C ++ that we need to use in a java program, for this we need to define its peer in java.

Native class

class print_hello{ public: void do_stuff(){std::cout<<"hello"<<std::endl;} } 

Peer class in java

  class PrintHello{ //Address of the native instance (the native object) long pointer; //ctor. calls native method to create //instance of print_hello PrintHello(){pointer = newNative();} //////////////////////////// //This whole class is for the following method //which provides access to the functionality //of the native class public void doStuff(){do_stuff(pointer);} //Calls a jni wrapper for print_hello.do_stuff() //has to pass the address of the instance. //the native keyword keeps the compiler from //complaining about the missing method body private native void do_stuff(long p); // //Methods for management of native resources. // //Native instance creation/destruction private native long newNative(); private native deleteNative(long p); //Method for manual disposal of native resources public void dispose(){deleteNative(pointer);pointer = 0;} } 

JNI Code (Incomplete)

All declared native methods require a built-in jni implementation. The following implements only one of the built-in methods described above.

 //the method name is generated by the javah tool //and is required for jni to identify it. void JNIEXPORT Java_PrintHello_do_stuff(JNIEnv* e,jobject i, jlong pointer){ print_hello* printer = (print_hello*)pointer; printer->do_stuff(); } 
+15
source share

A Java object has a peer / native object if it has some native methods written in C.

0
source share

All Articles