29

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)
3
  • 1
    hmm from docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html All classes have at least one constructor. If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, called the default constructor. This default the Object constructor if the class has no other parent. Commented Aug 22, 2012 at 5:56
  • 1
    Is it possible to accept more than one answer? Because I'd like to accept all 4 answers given below. Commented Aug 22, 2012 at 6:38
  • Strange, @shrini1000, you are right, it's not in the source code, i checked Java 1.8.0_241. But it's in the online java documentation, i checked from Java 13 all the way to 8, the Object() constructor is defined. Commented Feb 9, 2020 at 10:42

5 Answers 5

25

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.
}
Sign up to request clarification or add additional context in comments.

Comments

6

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();
}

Comments

6

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
}

Comments

6

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

Comments

2

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

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.