In OOP(Object-oriented programming) constructor is a special method. It is called whenever you create an object using new keyword. Constrictor enables an object to initialize itself at the time of its creation without the need to make a separate call to the instance method. It looks like a method but it is different from the method in two ways as follow
- A constructor always has the same name as the class whose instance members they initialize.
- It also does not have a return type and not even void like methods. It causes the compiler to automatically call the constructor whenever an object of the class is created.
Syntax for Constructor
constructorName([parameterList])
{
//constructor body
}
- Here, the constructorName is the same as the class name it belongs to.
- The parameterList is the list of optional zero or more parameters that are specified after the classname in the parentheses.
Create Constructor:
// Create a Main class
public class Main {
int x;
// Create a class constructor for the Main class
public Main() {
x = 5;
}
public static void main(String[] args) {
Main myObj = new Main();
System.out.println(myObj.x);
}
}
Output:
5
Constructor in OOP:
Default Constructor:
Whenever we create an object, a constructor is invoked. In case we did not create any constructor, the compiler will automatically write one for us. This constructor known as default constructor. It does not have any parameters and any statement in its body. The main aim of default constructor is to enable you to create an object of a class type. When the compiler create a default constructor, It does nothing. The objects created the default constructor will have fields with their default values.
class Display {
int a;
boolean b;
public static void main(String[] args) {
// A default constructor is called
Display obj = new Display();
System.out.println("Default Value:");
System.out.println("a = " + obj.a);
System.out.println("b = " + obj.b);
}
}
Output:
a = 0
b = false
Parameterized Constructor:
Unlike default constructor, parameterized constructor have one or more parameters. This type of constructor, it is possible to initialize objects with different set of values at the time of creation. These different set of value initialized to objects must pass as an arguments when the constructor is invoked. The list of parameter can be specified in the parentheses in the same way as parameters are specified in the methods.
class Display {
String languages;
// constructor accepting single value
Display(String lang) {
languages = lang;
System.out.println(languages + " Programming Language");
}
public static void main(String[] args) {
// call constructor by passing a single value
Display obj1 = new Display("Java");
Display obj2 = new Display("Python");
Display obj3 = new Display("C");
}
}
Output:
Java Programming Language
Python Programming Language
C Programming Language
Recommended:
What is variable in JavaScript?
How to define constant in java?
constructor method play a very important role in each OOP language. Nice post