Constructor in java

Rachna Bhadauria
1 min readApr 11, 2019

Constructor in java is used to create the instance of the class. Constructors are almost similar to methods except for two things — it’s name is same as class name and it has no return type. Sometimes constructors are also referred as special methods to initialize an object.

Whenever we use new keyword to create an instance of class, constructor is invoked and object of class is returned. Since constructor can only return object to class, it’s implicitly done by java runtime and we are not supposed to add return type to it.

If we add return type to a constructor, then it will become a method of class. This is the way java runtime distinguish between a normal method and a constructor. Let’s assume we have following code in Employee class.

Here first one is a constructor, notice that there is no return type and no return statement. Second one is a normal method where we are again calling the first constructor to get Employee instance and return it.

--

--