Constructors in Java with Examples

Classes have constructors by default. When an object is created, it gets initialized through a constructor. As an example, let’s say you want to automatically initialize the length and width of a rectangle when the object gets created, then use constructors. In other words, when an object of a class gets created, the constructor gets called.

If no constructor is present in the class, the default constructor gets called. The default constructor initializes all member variables to zero, but it’s not used when a constructor is created by the user.


Read More: Learn what are Classes and Objects in Java


Always remember the Constructor rules:

  • The constructor name is the same as the class name.
  • If you haven’t created a constructor, Java automatically creates one for you, known as the default constructor.
  • Constructors have no explicit return type.
  • Constructors are syntactically similar to methods.

Let us now look into the types of constructors. Constructors in Java are of two types:

  • Default
  • Parameterized.

Default Constructor in Java

If a user hasn’t defined a constructor in a class, then the Java compiler automatically generates a Default Constructor. The Default constructor gets called when the object is created. However, on defining your own constructor in the class, the existence of Default ends.

When Default Constructor is defined, all the fields are set their initial value of false for boolean type, 0 for integer types, 0.0 for floating-point types, etc. i.e. depending on their type.

Default Constructor Example

Let us now see an example wherein we will be displaying the default value of field values with Default Constructor automatically generated by the Java Compiler:

The output is as follows:

In the above code, since we haven’t defined a constructor, the default constructor is automatically generated by the Java compiler.  Additionally, the concept can be understood using the below image:

Default Constructors in Java

With the Default Constructor, all the fields are set to their initial value i.e. we have double values (length and width) above, which will be set to default 0.0. The same is visible in the output above for length and width:

Parameterized Constructor in Java

Parameterized Constructor is a constructor with a particular number of parameters. This is useful if you have different objects and you want to provide different values.

First, let us see what a parameterized constructor looks like with 2 parameters:

Parameterized Constructor Example

Let us now see an example wherein we have created Parameterized constructor and displayed the field values:

The output is as follows:

Now, let us understand the flow of parameterized constructors in the above program.

In the above code, we created 3 objects of the Rectangle class. As the parameters, we passed the length and width of the Rectangle to the parameterized constructor:

The length and width values pass to the parameterized constructor:

Then the display function is called from all the 3 objects to show the different values:

The display() function displays the length and breadth of the 3 objects of the Rectangle:

In this tutorial, we saw what are Constructors in Java, used and what are its types. Moreover, we also learned about the Default and Parameterized Constructors.

Java Classes and Objects
Java Virtual Machine (JVM)
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading