What Is A Native Method
What Is A Native Method
In Java, this is done via native methods. In your Java class,
you mark the methods you wish to implement outside of Java
with the native method modifier-much like you would use the
public or static modifiers
Then, rather than supplying the method's body, you simply
place a semicolon in its place. As an example, the following
class defines a variety of native methods
Platform Neutrality
Security Concerns
System Safety
Dependence on the Java Implementation
A Java Class with Native
Methods
simple look at a Java class containing native methods. This class encapsulates the
base and height of a triangle and contains a native method named ComputeArea that
calculates the area of the triangle
Triangle.java.
public class Triangle {
public void SetBase(float fInBase) {
fBase = fInBase;
}
public void SetHeight(float fInHeight) {
fHeight = fInHeight;
}
public native float ComputeArea();
// Load the native routines.
static {
System.loadLibrary("Triangle");
}
float fBase;
float fHeight;
}
A Java Class with Native
Methods
Compiling the Java Class
C:\java\classes\Triangle> javac Triangle.java
C:\java\classes\Triangle>
Using javah to Generate the Header File
C:\java\classes\Triangle> javah Triangle.java
C:\java\classes\Triangle>
Using javah-stubs to Generate a Stubs File
C:\java\classes\Triangle> javah -stubs Triangle
C:\java\classes\Triangle>