1

So I came across a method like this in some source

public native void method();

There was code inside, but it was commented out,

/*MANUAL code code code */

The code looked a bit like C++. So what does this mean? I know it does something because eventually it was called in the class...

4 Answers4

4

That's a declaration for a native method, indicated by the native modifier, as explained in the Java Language Specification, section §8.4.3.4 native Methods .

You won't find its implementing code in Java, it's referring to code that's either in the C libraries that are part of the implementation of the JDK in use, or part of a native library bundled with the code you're studying. Refer to the JNI documentation to understand how this gets called from a Java program.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
2

The native keyword in Java signifies the method is written in another language. See this exact question: What is the native keyword in Java for?

Community
  • 1
  • 1
Kon
  • 10,702
  • 6
  • 41
  • 58
1

the keyword native states that this method's implementation comes from a native language like c/cpp etc via JNI

A common example: a couple of methods in java.lang.Object class are implemented in native language.

rocketboy
  • 9,573
  • 2
  • 34
  • 36
0

The native keyword is applied to a method to indicate that the method is implemented in native code using JNI. It marks a method, that it will be implemented in other languages, not in Java in other word, native is implemented in platform-dependent code, typically written in another programming language.

You can find more example from here.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115