| Implementing Native Methods

## The Method Signature and the Function Signature

> 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 the `close()` methods in both the InputFile and the OutputFile classes. In both classes, the declaration for `close()` looks like this:

> > >         // in both InputFile.java and OutputFile.java
> > >     public native void close();

> > This method signature is similar to the signatures for regular, non-native Java methods. The difference is the `native` keyword and the fact that there is no implementation for this method (no code between curly brackets { and }). The `native` 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 the `void` 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 the `close()` method for both the InputFile class and the OutputFile class. You can get the function signatures for the native language functions by using `javah` to generate a header file.

> > The function signature for InputFile's `close()` function looks like this:

> > >         // in InputFileImpl.c
> > >     void InputFile_close(struct HInputFile *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 signature for OutputFile's `close()` method looks like this:

> > >         // in OutputFileImpl.c
> > >     void OutputFile_close(struct HOutputFile *this)
> > >         . . .

> > The function names are different: each contains the class name for which this is a native method implementation.

> > Notice that both `InputFile_close()` and `OutputFile_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 the `this` 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 Java data type is mapped to the nearest matching data type on the native language side. Returning a Value from a Native Method shows you how to match the Java and native language data types of the return value.

| Implementing Native Methods