What is Constructor ?
A constructor is used for automatically initializing an object when that particular object is created, enabling us to provide values to the instance variable before assigning the object to the reference variable.
Syntactically, it looks like a method with no return type, not even a Void. If a user doesn’t define any constructor explicitly then at the time of execution of a program, Java compiler automatically makes that object usable immediately.
Key Points to Remember:
- It has the same name as the class name.
- It automatically gets called when an object is instantiated.
- It provides default values to an instance variable.
- It has no return type, not even void.
- If no constructor has been defined, Java compiler automatically calls the default constructor.
- Java provided default constructor always initialize instance variables with their default values. Numeric types = 0 , Reference type = null and boolean type = false.
Syntax:
ClassName(argument) { //body }
Java program to demonstrate the working of a constructor
class Sample { int x; Sample () { x = 5; } void display () { System.out.println (" Value of x = " + x); } } public class ConstructorExample { public static void main (String[]args) { Sample obj = new Sample (); obj.display (); } }
Output:
Types of Java Constructors
In Java, a constructor can be categorized into two types, namely Default Constructor or No-Argument Constructor and Parameterized Constructor.
Default Constructor or No-Argument Constructor: A constructor with no parameter is termed as a default constructor.
In Java, if no constructor is defined, a default constructor is explicitly created and invoked by the compiler, in such case, JVM assigns a default value to an instance variable i.e for String, null, for integer, 0.
When a user provides a default constructor explicitly, then it is usually intended for initializing instance variables with some default values.
Parameterized Constructor: A constructor with a parameter is termed as a parameterized constructor which enables a user to provide different values to an object on every creation. It provides flexibility in initializing different values to a different object.
Can we Overload a Constructor in Java?
It is possible to overload a constructor in Java. Constructor overloading is a feature which allows defining two or more than two constructors having different parameter list in order to carry out different behavior.
Like methods, constructors can also be overloaded where multiple constructors are declared with different parameters.
Java program to overload a constructor
class Sample { int x; int y; Sample () { x = 5; y = 5; } Sample (int num) { x = num; y = 5; } Sample (int num, int num1) { x = num; y = num1; } void display () { System.out.println (" Value of x = " + x + " Value of Y = " + y); } } public class ConstructorExamples { public static void main (String[]args) { Sample obj = new Sample (); Sample obj1 = new Sample (7); Sample obj2 = new Sample (6, 10); obj.display (); // default constructor obj1.display (); // parameterized constructor with 1 arguement obj2.display (); // parameterized constructor with 2 arguements } }
Output: