What are Constructors ?
In Java, a Constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling the constructor, memory for the object is allocated in the memory. It is a special type of method that is used to initialize the object. Every time an object is created using the new() keyword, at least one constructor is called.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // Create a Main class public class Main { int x; // Create a class attribute // Create a class constructor for the Main class public Main() { x = 5; // Set the initial value for the class attribute x } public static void main(String[] args) { Main myObj = new Main(); // Create an object of class Main (This will call the constructor) System.out.println(myObj.x); // Print the value of x } } |
Output:
1 | 5 |
Types of Constructors
Now is the correct time to discuss the types of the constructor, so primarily there are three types of constructors in Java are mentioned below:
- Default Constructor
- Parameterized Constructor
- Copy Constructor
Default Constructor
A constructor that has no parameters is known as default the constructor. A default constructor is invisible.
Syntax:
// body of the constructor
}
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 | //Java Program to create and call a default constructor class Wrap{ //creating a default constructor Wrap(){ System.out.println("Default Constructor"); } //main method public static void main(String args[]){ //calling a default constructor Wrap wrap =new Wrap(); } } |
Output:
1 | Default Constructor |
Parameterized Constructor
A constructor that has parameters is known as parameterized constructor. If we want to initialize fields of the class with our own values, then use a parameterized constructor.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // Java Program for Parameterized Constructor class Wrap { // data members of the class. String name; int id; Wrap(String name, int id) { this.name = name; this.id = id; } } class WL { public static void main(String[] args) { // This would invoke the parameterized constructor. Wrap wrapp = new Wrap("Avinash", 68); System.out.println("WrapName :" + wrapp.name + " and WrapId :" + wrapp.id); } } |
Output:
1 | WrapName :Avinash and WrapId :68 |
Copy Constructor
Unlike other constructors copy constructor is passed with another object which copies the data available from the passed object to the newly created object.
Ex:
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 32 33 34 35 36 37 38 39 40 41 | // Java Program for Copy Constructor import java.io.*; class Geek { // data members of the class. String name; int id; // Parameterized Constructor Geek(String name, int id) { this.name = name; this.id = id; } // Copy Constructor Geek(Geek obj2) { this.name = obj2.name; this.id = obj2.id; } } class GFG { public static void main(String[] args) { // This would invoke the parameterized constructor. System.out.println("First Object"); Geek geek1 = new Geek("Avinash", 68); System.out.println("GeekName :" + geek1.name + " and GeekId :" + geek1.id); System.out.println(); // This would invoke the copy constructor. Geek geek2 = new Geek(geek1); System.out.println( "Copy Constructor used Second Object"); System.out.println("GeekName :" + geek2.name + " and GeekId :" + geek2.id); } } |
Output:
1 2 3 4 5 | First Object GeekName :Avinash and GeekId :68 Copy Constructor used Second Object GeekName :Avinash and GeekId :68 |
Constructor Overloading
In Java, a constructor is just like a method but without return type. It can also be overloaded like Java methods.
Ex:
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 | class Student { int id; String name; int age; //creating two arg constructor Student(int i, String n) { id = i; name = n; } //creating three arg constructor Student(int i,String n,int a) { id = i; name = n; age=a; } void display(){ System.out.println(id+" "+name+" "+age); } public static void main(String args[]){ Student s1 = new Student(111,"Karan"); Student s2 = new Student(222,"Aryan",25); s1.display(); s2.display(); } } |
Output:
1 2 | 111 Karan 0 222 Aryan 25 |
Difference between constructor and method
Java Constructor | Java Method |
---|---|
A constructor is used to initialize the state of an object. | A method is used to expose the behavior of an object. |
A constructor must not have a return type. | A method must have a return type. |
The constructor is invoked implicitly. | The method is invoked explicitly. |
The Java compiler provides a default constructor if you don’t have any constructor in a class. | The method is not provided by the compiler in any case. |
The constructor name must be same as the class name. | The method name may or may not be same as the class name. |