Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) 
like image 917
shrini1000 Avatar asked Aug 22 '12 05:08

shrini1000


People also ask

Does object have a constructor?

Object() constructorThe Object constructor turns the input into an object. Its behavior depends on the input's type. If the value is null or undefined , it will create and return an empty object. Otherwise, it will return an object of a Type that corresponds to the given value.

How many constructors are there in object class in Java?

In such case, Java compiler provides a default constructor by default. There are two types of constructors in Java: no-arg constructor, and parameterized constructor. Note: It is called constructor because it constructs the values at the time of object creation. It is not necessary to write a constructor for a class.

Can we use object without constructor in Java?

Actually, yes, it is possible to bypass the constructor when you instantiate an object, if you use objenesis to instantiate the object for you. It does bytecode manipulations to achieve this. Deserializing an object will also bypass the constructor.

What is the object class constructor?

In class-based, object-oriented programming, a constructor (abbreviation: ctor) is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.

What is a constructor in Java?

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes:

Does an object class have a default constructor?

Yes,Object class have a default constructor as the doc says. 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.

When is the constructor of a class called?

The constructor is called when an object of a class is created. It can be used to set initial values for object attributes: Note that the constructor name must match the class name, and it cannot have a return type (like void ). Also note that the constructor is called when the object is created.

What is the return type of a constructor in Java?

A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type.


1 Answers

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. } 
like image 96
Aleksander Blomskøld Avatar answered Sep 21 '22 17:09

Aleksander Blomskøld