ktrueda
ktrueda

Reputation: 11

Where is definition of Method java/lang/Object."<init>":()V?

I am trying to implement JVM(Java8) by python as a study.

I want to execute java Main. In main method, Person instance will be initialized. Then Person class constructor invoke special Method java/lang/Object."<init>":()V. But I can't find the definition of that method although I saw JVM spec.

Main.java

class Main{
  public static void main(String[] args){
    Person p = new Person("Jeff", 17);
  }
}

Person.java

public class Person {
  private String name;
  private int age;
  public Person(String name, int age){
    this.name = name;
    this.age = age;
  }
}

To compile, I used javac -encoding UTF-8 -target 8 -source 8 *.java.

javap -v Main shows below

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: (0x0009) ACC_PUBLIC, ACC_STATIC
    Code:
      stack=4, locals=2, args_size=1
         0: new           #7                  // class Person
         3: dup
         4: ldc           #9                  // String Chisato
         6: bipush        17
         8: invokespecial #11                 // Method Person."<init>":(Ljava/lang/String;I)V
        11: astore_1
        12: return

javap -v Person shows below

  public Person(java.lang.String, int);
    descriptor: (Ljava/lang/String;I)V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=2, locals=3, args_size=3
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: aload_0
         5: aload_1
         6: putfield      #7                  // Field name:Ljava/lang/String;
         9: aload_0
        10: iload_2
        11: putfield      #13                 // Field age:I
        14: return

Upvotes: 1

Views: 1091

Answers (2)

rzwitserloot
rzwitserloot

Reputation: 103502

That's JVM-ese efor a method reference. <init> means 'constructor'. ()V means: A method that takes no arguments and returns void (as in, nothing).

In other words this tuple of strings:

java/lang/Object <init> ()V

refers to the no-args constructor of java.lang.Object itself. Which definitely exists, it's part of all java distributions.

The reason it is called here is basic java reasons:

  • ALL classes MUST have a constructor.
  • ALL constructors MUST begin by invoking either another constructor in the same class, or one of the super constructors. (this(); or super(); in java).
  • If you fail to write a constructor, java assumes you meant to write public MyClass() {}.
  • If you fail to write a this() or super() on the first line, java assumes you meant to write super(); and will act as if you did.

Thus, your Person class has a constructor, and it begins by invoking its parent class's no-args constructor.

Which is java/lang/Object <init> ()V.

The only class that is supposed to break this rule is java.lang.Object itself which has no parent class and doesn't do this. With bytecode hackery you can make classes that break these rules, and a JVM will run that just fine if you really want to. But don't - code in the JVM ecosystem expects you to do these things.

Upvotes: 2

Andy Turner
Andy Turner

Reputation: 140504

A look at the source code of java.lang.Object (e.g. http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/tip/src/share/classes/java/lang/Object.java) shows that there are no explicit constructors in the Object class. As such, Object has a default constructor.

As stated in JLS 8.8.9:

If a class contains no constructor declarations, then a default constructor is implicitly declared. The form of the default constructor for a top level class, member class, or local class is as follows:

  • ...
  • If the class being declared is the primordial class Object, then the default constructor has an empty body.

So, Object.<init>() is simply a constructor with an empty body.

In more recent versions (e.g. OpenJDK head), there is an explicit constructor, but, again, it has an empty body:

    @IntrinsicCandidate
    public Object() {}

Upvotes: 1

Related Questions