2

In the Object.class file in JDK 7 I see the following snippet

public class Object {

    private static native void registerNatives();
    static {
        registerNatives();
    }

two questions:

  1. Ok where is the actual implementation of this method? the method has no body.
  2. The method is already set as static. So why is that static block needed again right below it?
  • The "native" keyword indicates the method is implemented.... natively.: http://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/jniTOC.html – Alexandre Santos Jun 25 '14 at 01:07

2 Answers2

2

This method declaration

private static native void registerNatives();

doesn't provide a body. It's similar to interface methods

public interface Whatever {
    public void someMethod();
}

The method isn't abstract, but the implementation is deferred to native code. Note that it is simply a method declaration. It hasn't been invoked.

To invoke it, Object declares a static initializer block. This will be executed when the class is loaded. Object is one of the system classes and is among the first classes that are loaded by your JVM.

Finding the native code depends on your JRE implementation. Where to find source code for java.lang native methods?

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0

1 native method is implemented in native code

A method that is native is implemented in platform-dependent code, typically written in another programming language such as C. The body of a native method is given as a semicolon only, indicating that the implementation is omitted, instead of a block.

See What are native methods in Java and where should they be used?

2. static block is used here to call this method

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438