Java Reference
In-Depth Information
package javatech.jni22;
public class
JNIHelloWorld
{
static
{
System.loadLibrary (
"
NativeHelloWorld
"
);
}
// Declare the two native methods.
public native void nativeHelloWorld ();
public static native void nativeHelloWorldStatic ();
public static void main (String[] args) {
// Call the static native method.
System.out.println (
"main: calling nativeHelloWorldStatic()");
nativeHelloWorldStatic ();
// Call the constructor, which will call the
// non-static method.
System.out.println (
"main: instantiating JNIHelloWorld");
new JNIHelloWorld ();
// Exit.
System.out.println (
"
main: exiting
"
);
}
// main
// Constructor
public JNIHelloWorld ()
{
// Call the non-static native method.
System.out.println (
"
ctor: calling nativeHelloWorld()
"
);
nativeHelloWorld ();
} // ctor
} // JNIHelloWorld
We have named the two native methods with the prefix
“
native
„
to make it
explicit that they are native methods. We've also named the static native method
with the suffix
“
Static
„
to distinguish it from the non-static method.
The only other new feature in this source code is the static initializer that
calls
System.loadLibrary()
. The purpose of
System.loadLibrary()
should be obvious - to load the native library that contains the native method
implementations. Recall that a static initializer runs when the class is first initial-
ized. (The details of class initialization are a bit esoteric, as well as almost always
unimportant to most Java programmers. It is sufficient to know that the JVM per-
forms initialization before a class is actually used. Therefore, loading the native
Search WWH ::
Custom Search