Constructor is a block of code that initializes the newly created object. A constructor resembles an instance method in java but it’s not a method as it doesn’t have a return type. In short constructor and method are different. Constructor declarations look like method declarations—except that they use the name of the class and have no return type.
Syntax:
class Simple
{
data member;
void display(){
// code to be executed
}
Simple() // constructor
{
// code to be executed
}
}
It has following Rules
1. Constructor name must be the same as its class name.
2. A constructor in Java can not be abstract, final, static and Synchronized.
3. A Constructor must have no explicit return type that means constructor doesn’t return any value.
There are two types of constructor available in Java:
Default constructor (no-arg constructor)
Parameterized constructor
A constructor that has no parameter is known as the default constructor. If we don’t define a constructor in a class, then the compiler creates default constructor(with no arguments) for the class.
Syntax:
className(){}
class Simple
{
Simple()
{
System.out.println("This is a default constructor");
}
public static void main(String args[]) {
Simple obj = new Simple();
}
}
Output:
This is a default constructor
A constructor which has a specific number of parameters is called a parameterized constructor. That mean, A constructor that has parameters is known as parameterized constructor.
Syntax:
className(list of parameters){}
class Employee {
int empId;
String empName;
//parameterized constructor with two parameters
Employee(int id, String name){
this.empId = id;
this.empName = name;
}
void displayInfo(){
System.out.println("Id: "+empId+" Name: "+empName);
}
public static void main(String args[]){
Employee obj1 = new Employee(45,"Peter");
Employee obj2 = new Employee(21,"Tony");
obj1.displayInfo();
obj2.displayInfo();
}
}
Output:
Id: 45 Name: Peter
Id: 21 Name: Tony
Like methods Overloading, we can also overload constructors for creating objects in different ways. Compiler differentiates constructors on the basis of numbers of parameters, types of the parameters.
class Student{
int id;
String name;
//creating one arg constructor
Student(int i)
{
id = i;
}
//creating two arg constructor
Student(int i,String n)
{
id = i;
name = n;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[]){
Student s1 = new Student(11);
Student s2 = new Student(2,"Stark");
s1.display();
s2.display();
}
}
Output:
11 null
2 Stark
Constructor | Method |
A constructor is used to initialized the instance variable of a class. | A method is use for any general purpose processing or calculation. |
A constructor must not have a return type. | A method must have a return type. |
A constructor name must be same as the class name. | A method name may or may not be same as the class name. |
A constructor is called only once at the time of Object creation | A method(s) can be called any numbers of time. |
A constructor is invoked implicitly. | A method is invoked explicitly. |