A constructor is a block of codes just like a method in Java. It is a special type of method that is called when an instance of the class (that is object) is created and is used for the initialization of the data members of a newly created object.
There are some rules for Java constructor:
- The constructor name must be the same as its class name.
- It should not have any return type not even void.
Types of Constructors in java:
- Default constructor(no-argument constructor)
- Parameterized constructor.
Default constructor(no-argument constructor):
This constructor does not have any parameter and is hence called a no-argument constructor.
Syntax of default constructor in Java:
1 2 3 4 5 6 7 | public class className { className() //default constructor { ...... } } |
Basic Example: Program to create and call a default constructor in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class Car { //creating a default constructor Car() { System.out.println("Car is created"); } public static void main(String args[]) { //calling a default constructor Car c = new Car(); } } |
Output of Default constructor:
Car is created
Note: Default Constructor is used if the user does not define a constructor then the java compiler creates a default constructor and initializes the member variables to its default values. That is numeric data types are set to 0, char data types are set to the null character.
Basic Example for displaying the default values in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class Employee { int id; String name; int age; //Method for Displaying default values void display() { System.out.println(id + " " + name + " " + age); } public static void main(String args[]) { //creating objects Employee e1 = new Employee(); Employee e2 = new Employee(); e1.display(); e2.display(); } } |
Output:
0 null 0
0 null 0
Java Parameterized Constructor:
A Parameterized Constructor is a constructor having a specific number of parameters. The parameter is added to a constructor as they are added to the methods that are inside the parentheses of the constructor.
The data type must match with values that are passed and in that particular order and number.
Example of Parameterized Constructor in Java:
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 | public class Employee { int id; String name; int age; //parameterized constructor Employee(int i, String n, int a) { id = i; name = n; age = a; } //display the values void display() { System.out.println(id + " " + name + " " + age); } public static void main(String args[]) { //creating objects and passing values Employee e1 = new Employee(101, "John", 30); Employee e2 = new Employee(105, "Ron", 32); //calling display method e1.display(); e2.display(); } } |
Output:
101 John 30
105 Ron 32
Constructor Overloading in Java:
Constructor Overloading is like a method of overloading in Java. This is a technique in Java where a single class has more than one Constructor and each of them has a different parameter. The number of parameters and their data types differs for each constructor by the compiler.
Example of Constructor Overloading in Java :
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 42 43 44 45 | public class Employee { int id; String name; int age; //parameterized constructors Employee(int i, String n, int a) //three parameters { id = i; name = n; age = a; } Employee(int i, String n) //two parameters { id = i; name = n; } //displaying three parameterized constructor void display1() { System.out.println("Displaying three parameterized constructor "); System.out.println(id + " " + name + " " + age); } //displaying two parameterized constructor void display2() { System.out.println("\nDisplaying three parameterized constructor "); System.out.println(id + " " + name); } public static void main(String args[]) { //creating objects and passing values Employee e1 = new Employee(101, "Prashant", 30); Employee e2 = new Employee(105, "Karan"); //calling display method for each constructor e1.display1(); e2.display2(); } } |
Output:
Displaying three parameterized constructor
101 Prashant 30
Displaying three parameterized constructor
105 Karan