[ACCEPTED]-Java: does Object class have a constructor?-constructor

Accepted answer
Score: 25

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

A 1 more explicit version of your code:

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

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

Your code is equivalent 1 to writing:

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

You do not see it in the stack trace because 3 the constructor of the super class is called 2 before your new Throwable().printStackTace() call. What the compiler actually 1 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
Score: 6

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

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

C:\stackdemo>javap -c ConTest
Compiled from "ConTest.java"
public class ConTest extends java.lang.Object{
public ConTest();
   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[]);
   0:   new     #5; //class ConTest
   3:   dup
   4:   invokespecial   #6; //Method "<init>":()V
   7:   astore_1
   8:   return


Thank you

Score: 2

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

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

More Related questions