| Table of Contents

# Implementing Native Methods

> A native method has two components: the declaration and the implementation. You declare a native method, like other methods, in a Java class. You implement a native method in another programming language in a different source file. Typically, the implementation of a native method is a function.

> When writing a native method you will probably want to pass various arguments into the method and return values from it. In addition, your native method might want to access Java objects' member variables and call Java objects' methods. This lesson shows you how to do all of these things.

> #### The Character Replacement Program

> > This lesson is focused around a small example that implements a character replacement program. The program uses two Java classes, InputFile and OutputFile, that implement a readable file and a writable file respectively. Both of these classes have three native methods. Before delving into the specifics of implementing a native method let's get familiar with this example.

> #### The Method Signature and the Function Signature

> > On the Java side, you declare a native method within a Java class. On the native language side, you provide an implementation for the native method. You must take care when writing native methods to "match" the method signature in Java with the function signature on the native language side.

> #### Passing Data into a Native Method

> > You can pass any type of argument into a native method. This includes arguments of a primitive data type, such as `int`, `float`, `boolean`, and arguments of a reference data type, such as arrays, strings, and objects.

> #### Returning a Value from a Native Method

> > The return value from a native method must be interpreted correctly on the Java side. In addition, you can return data through the arguments of a native method.

> #### Using a Java Object in Native Method Implementations

> > Once you've passed an object into a native method, you're likely to want to use it. This page discusses a collection of utility macros and functions that allow you to access an object's member variables and call an object's methods from within a native method implementation.

> #### Working with Strings

> > This section discusses a collection of utility functions that allow you to manipulate Java strings within a native method implementation.

> #### Native Methods and Thread Synchronization

> > Native methods can be synchronized. This page shows you the native equivalent of the `wait()` and `notify()` methods critical in the synchronization of threads.

> #### Throwing Exceptions from Within a Native Method

> > Native methods can throw exceptions like regular Java methods can.

> #### Using C++ in Native Method Implementations

> > You can integrate C++ code into your Java code if you take special precautions to prevent name mangling.

| Table of Contents