26 May 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Rectangle { double length; double width; // displays the default value of double type void display() { System.out.println("Length = "+length); System.out.println("Width = "+width); } } public class StudyopediaDemo { public static void main(String args[]) { Rectangle rct = new Rectangle(); rct.display(); } } |
The output is as follows:
1 2 3 4 |
Length = 0.0 Width = 0.0 |
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:
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:
1 2 3 4 |
double length; double 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:
1 2 3 4 5 6 7 |
// parameterized constructor Rectangle (double len, double wid) { length = len; width = wid; } |
Parameterized Constructor Example
Let us now see an example wherein we have created Parameterized constructor and displayed the field values:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class Rectangle { double length; double width; // parameterized constructor Rectangle (double len, double wid) { length = len; width = wid; } void display() { System.out.println("Length = "+length); System.out.println("Width = "+width); } } public class StudyopediaDemo { public static void main(String args[]) { // 3 objects Rectangle rct1 = new Rectangle(5, 10); Rectangle rct2 = new Rectangle(8, 20); Rectangle rct3 = new Rectangle(10, 25); rct1.display(); rct2.display(); rct3.display(); } } |
The output is as follows:
1 2 3 4 5 6 7 8 |
Length = 5.0 Width = 10.0 Length = 8.0 Width = 20.0 Length = 10.0 Width = 25.0 |
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:
1 2 3 4 5 |
Rectangle rct1 = new Rectangle(5, 10); Rectangle rct2 = new Rectangle(8, 20); Rectangle rct3 = new Rectangle(10, 25); |
The length and width values pass to the parameterized constructor:
1 2 3 4 5 6 7 |
// parameterized constructor Rectangle (double len, double wid) { length = len; width = wid; } |
Then the display function is called from all the 3 objects to show the different values:
1 2 3 4 5 |
rct1.display(); rct2.display(); rct3.display(); |
The display() function displays the length and breadth of the 3 objects of the Rectangle:
1 2 3 4 5 6 |
void display() { System.out.println("Length = "+length); System.out.println("Width = "+width); } |
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.
No Comments