Native methods (native keyword)
A native method is a Java method whose implementation is written in another language, typically C or C++. The native keyword indicates this.
Why use native methods?
- Access platform‑specific features (OS, hardware).
- Reuse existing legacy code.
- Performance‑critical operations (e.g., cryptography, graphics).
Syntax:
public native returnType methodName(parameters);Example:
public class NativeDemo { static { System.loadLibrary("nativeLib"); // load shared library }
public native void sayHello();}Steps to create a native method:
- Write Java code with
nativemethod declaration. - Compile Java code:
javac NativeDemo.java - Generate C/C++ header:
javac -h . NativeDemo.java - Implement the native method in C/C++ using the generated header.
- Compile the native code into a shared library (
.dllon Windows,.soon Linux,.dylibon macOS). - Load the library using
System.loadLibrary().
C implementation example (simplified):
#include <jni.h>#include "NativeDemo.h"
JNIEXPORT void JNICALL Java_NativeDemo_sayHello(JNIEnv *env, jobject obj) { printf("Hello from C!\n");}Important notes:
- Native methods bypass Java’s security and memory management.
- They are not portable; each platform needs its own library.
- Java Native Interface (JNI) is the standard API for native methods.
- Use sparingly; prefer pure Java when possible.