shrini1000
shrini1000

Reputation: 7256

Java: does Object class have a constructor?

Javadoc mentions that Object class has a public no-arg constructor. But Object's source code doesn't have any explicit constructor in it. So obviously the compiler has generated one for it. However, if I see the call stack trace when a constructor is about to return (as shown below), I do not see any call to Object.<init> in that trace.

So the question is, does Object class have a default constructor as the doc says? If yes, why do I not see it in the call stack trace?

public ConTest()
{
    new Throwable().printStackTrace();
}

Result:

java.lang.Throwable
    at ConTest.<init>(ConTest.java:8)
    at ConTest.main(ConTest.java:16)

Upvotes: 29

Views: 13359

Answers (5)

Kuldeep S. Mahajan
Kuldeep S. Mahajan

Reputation: 570

Yes,Object class have a default constructor as the doc says.

As you know insted of doing that you can check it by using javap -c ConTest in command prompt you can see it's calling the object class default constructor() in below's code's Line No:1

C:\stackdemo>javap -c ConTest
Compiled from "ConTest.java"
public class ConTest extends java.lang.Object{
public ConTest();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   new     #2; //class java/lang/Throwable
   7:   dup
   8:   invokespecial   #3; //Method java/lang/Throwable."<init>":()V
   11:  invokevirtual   #4; //Method java/lang/Throwable.printStackTrace:()V
   14:  return

public static void main(java.lang.String[]);
  Code:
   0:   new     #5; //class ConTest
   3:   dup
   4:   invokespecial   #6; //Method "<init>":()V
   7:   astore_1
   8:   return

}

Thank you

Upvotes: 6

Amit Deshpande
Amit Deshpande

Reputation: 19185

As Suggested above super() is the first call in the constructor and for method More Information here

When you compile a class, the Java compiler creates an instance initialization method for each constructor you declare in the source code of the class. Although the constructor is not a method, the instance initialization method is. It has a name, <init>, a return type, void, and a set of parameters that match the parameters of the constructor from which it was generated

Upvotes: 2

Aleksander Blomsk&#248;ld
Aleksander Blomsk&#248;ld

Reputation: 18562

Super constructors are run before sub/base constructors. In your example Object's constructor has already been run when new Throwable().printStackTrace() is executed.

A more explicit version of your code:

public ConTest()
{
    super();
    new Throwable().printStackTrace(); // you will not see super() (Object.<init>) in this stack trace.
}

Upvotes: 25

Andreas Fester
Andreas Fester

Reputation: 36649

You do not see it in the stack trace because the constructor of the super class is called before your new Throwable().printStackTace() call. What the compiler actually creates is following .

public ConTest()
{
    super();   // This is the call to the base class constructor
    new Throwable().printStackTrace();   // already back from the base class constructor
}

Upvotes: 6

Strelok
Strelok

Reputation: 51481

You do not see it in the stack trace, because it was already called. The exception is thrown in your code.

Your code is equivalent to writing:

public ConTest() {
  super(); // this will call the Object constructor
  new Throwable().printStackTrace();
}

Upvotes: 6

Related Questions