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?
1

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.