| Implementing Native Methods

## Using C++ in Native Method Implementations

> The `javah` program generates C header and source files, which provide the glue for integrating C code with Java. You can use these C header and source files to integrate C++ code into Java as well.

> To do so, you must ensure that C++ does not perform any name mangling on the C function names. If you are using the JDK 1.0.2 release, `javah` automatically does this for you by framing the function declarations in the generated header files within an `extern "C"` block. If you are using a release prior to the JDK 1.0.2 release you must do this by hand. Here's a version of the header file generated by `javah` for the NativeExample class used in various examples throughout this lesson that has been modified to work with C++:

> >     /* DO NOT EDIT THIS FILE - it is machine generated */
> >     #include
> >     /* Header for class NativeExample */

> >     #ifndef _Included_NativeExample

> >     typedef struct ClassNativeExample {
> >         long myValue;
> >     } ClassNativeExample;
> >     HandleTo(NativeExample);

> >     extern "C" {
> >     struct Hjava_lang_String;
> >     extern struct Hjava_lang_String *NativeExample_quote(struct HNativeExample *,long);
> >     extern long NativeExample_twoTimes(struct HNativeExample *);
> >     struct HNativeExample;
> >     extern struct HNativeExample *NativeExample_doubleUp(struct HNativeExample *);
> >     }

> The bold lines indicate code that was added to the header file after generation by `javah`. Note that if you run `javah` after you modify a header file, you will have to modify the file again.

> The implementations for native methods may not be embedded in C++ classes. They must be global functions.

| Implementing Native Methods