![]() ![]() ![]() ![]() |
Implementing Native Methods |
This page shows you how to set up the two sides of a native method: the Java side and the native language side.The Java Side
Let's take a look at theclose()
methods in both the InputFile and the OutputFile classes. In both classes, the declaration for theclose()
looks like this:This method signature is similar to the signatures for regular, non-native Java methods. The difference is the// in both InputFile.java and OutputFile.java public native void close();native
keyword and the fact that there is no implementation for this method (no code between curly brackets { and }). Thenative
keyword informs the compiler that the implementation for this method is provided in another language. Because there is no implementation for this method in the Java source file, the declaration is terminated with a semicolon (;)--the statement terminator symbol.As with other Java methods, you can pass arguments into native methods and return values from them. The
close()
method does neither as indicated by thevoid
return type and the empty parameter list. Other methods in the InputFile and OutputFile classes accept arguments and return values and are discussed in later sections of this lesson.The Native Language Side
On the native language side, you must declare and implement theclose()
method for both the InputFile class and the OutputFile class. The first step is to write the signatures for each of the functions that will implement the twoclose()
methods. The function signature for InputFile'sclose()
function looks like this:Notice the name of the function--it's comprised of the name of the class, an underscore (_) character, and the name of the native method as declared in the class. So, as you might surmise, the function signarture for OutputFile's// in InputFileImpl.c void InputFile_close(struct HInputFile *this) . . .close()
method looks like this:Notice that the function names are different: each contain the class name for which this is a native method implementation.// in OutputFileImpl.c void OutputFile_close(struct HOutputFile *this) . . .Notice that both
InputFile_close()
andOutputFile_close()
accepts an argument even though no argument was declared for these methods on the Java side. The Java runtime system always passes an automatic parameter as the first argument to a native method. This argument is a handle to the object that contains the native method to which this native language function is bound. You can think of the first argument as thethis
pointer. There's more information about this argument on the next page.The return value for these two functions is
void
because neither returns a value. This is identical to the return type declared on the Java side. However, don't let this mislead you. When you actually return a value from a native method, you must be careful because the data type of the return value on the native language side may not be the same as the return value on the Java side. Returning a Value from a Native Method shows you how to return values from a native method and match the Java and native language data types of the return value.
![]() ![]() ![]() ![]() |
Implementing Native Methods |