Pandiyan Mani
1 min readAug 9, 2021

--

Constructor

Its a block of code similar to of method but it will called at the time of creation of object we dont need to call it explicitly by calling is method using object name.

Its mainly used for object intalization at the start of class

There are two types of constructor

1.Default constructor(with no arguments)

public class Employee
{
public static void main(String[] args) {
//singleton obj = new singleton();
header obj = new header();
}
}
class header
{
header()
{

}
}

2.Parameterized constructor(with no arguments)

The default constructor will be created by the compiler which will be null

Parameterized constructor the constructor with arguments

public class Employee
{
public static void main(String[] args) {
//singleton obj = new singleton();
header obj = new header(5,6);
}
}
class header
{
header(int a ,int b)
{

}
}

Constructor overloading is also possible with constructor having different paramter

public class Employee
{
public static void main(String[] args) {
//singleton obj = new singleton();
header obj = new header(5,6);
header obj1 = new header(5,6,7);
}
}
class header
{
header(int a ,int b)
{

}
header(int a ,int b,int c)
{

}
}

--

--