![]() |
VOOZH | about |
The native keyword in Java is applied to a method to indicate that the method is implemented in native code using JNI (Java Native Interface). The native keyword is a modifier that is applicable only for methods, and we can’t apply it anywhere else. The methods which are implemented in C, C++ are called native methods or foreign methods.
The native modifier indicates that a method is implemented in platform-dependent code, often seen in C language.
Conclusion: Java application scan call code is written in C, C++, or assembler.
The steps for the creation of native methods are as follows:
Let us first take random Java code that contains the native method and later we will be compiling it. We are done with the above two steps. For steps 3 and 4 we will be using the existing .exe known as java HEdge" in order to create a C header and C stub file.
Now we will insert(write) our C code(or use) and later using DLL, we will be creating objects of the same inside our application( Main example1A) and later calling the native methods thereby within the java program.
Example 1-A: Application
For the above program C++ code that is shared in DLL is as follows:
Example 1-B: Support to the above example
10
Note: DLL is named as can be perceived from program 1A: NameOfDLLFile
Before native method: var = null
After native method: var = 10
Do remember that there are certain important points about native keywords, which are as follows:
Note: Do go through strictfp keyword of java as is one of the concept which even very good java developer is unaware of.
In this section, we explain how to declare a native method in Java and how to generate the corresponding C/C++ function prototype.
Syntax: Declaring Native Methods
private native String getLine(String prompt);Syntax: From the Native Language Side
javah -jni Prompt
JNIEXPORT jstring JNICALL Java_Prompt_getLine(JNIEnv *, jobject, jstring);