What is a constructor ?
Constructor is a block of code which is called when an instance of the object is created and memory is allocated for that object.
Constructor constructs the values at the time of object creation.
If there is not constructor in your class, JRE will create a default constructor for the class.
Why do we use a constructor ?
The main purpose is to initialise the object of the class.
What are the rules to be followed for creating a constructor ?
1. Constructor name should have the same as the name of the class.
2. Constructor should not have any return type.
Difference between method and constructor ?
Constructor can create and initialise an object which doesn’t exists, But methods executes its operation with the already existing objects.
Constructors cannot be called directly, it can be called when new() keyword creates an object, where as the methods can be called directly to the object.
Constructors names should be the same name as of the class name, where as in Methods any name can be defined.
Constructor should not have the return type when its declared, where as the Method should be declared with the return type.
What is the syntax of the constructor ?
Syntax :
ConstructorName([parameterList])
{
//constructor body
}
ConstructorName should the same as that of the class name.
How to call a constructor ?
A constructor is called when an object of the class created with new – keyword.
Examples :
public class EgConstructor{
EgConstructor()
{
System.out.println("Inside the constructor class Eg1");
}
public static void main(String args[])
{
new EgConstructor();
}
}
Output :
Inside the constructor class Eg1
Example :
public class EgConstructor{
EgConstructor()
{
System.out.println("Inside the constructor class Eg1");
}
public static void main(String args[])
{
Eg1 obj = new Eg1();
}
}
Output :
Inside the constructor class Eg1
What is a default constructor ?
In java program if there is not constructor defined, the java compiler by default creates the object of the class thereby creates the constructor.
This newly created default constructor is called default constructor.
Example :
EgConstructor()
{
System.out.println("Inside the constructor class Eg1");
}
public static void main(String args[])
{
new EgConstructor();
}
}
Output :
Inside the constructor class Eg1
How the output is printed ?
When executed the program what java compiler creates the default constructor with the same name as that of the class name.
EgConstructor()
and when we create an object by
new EgConstructor();
It will print the output as
Output :
Inside the constructor class Eg1
What are the types of constructor ?
There are two types of constructors
1. Non-Parameterized constructor
2. Parameterized constructor
1. Non-Parameterized constructor :
If a constructor don’t have any parameters passed inside its called Non-Parameterized constructor.
Example :
public class EgConstructor{
public static void main(String args[])
{
System.out.println("Inside the constructor class Eg1");
EgConstructor object = new EgConstructor(); // No params passed for this constructor
}
EgConstructor()
{
int a = 10;
int b = 20;
int c = a+b;
System.out.println("the value of a = " +a);
System.out.println("the value of b = " +b);
System.out.println("the value of c = " +c);
}
}
Output :
Inside the constructor class Eg1
the value of a = 10
the value of b = 20
the value of c = 30
2. Parameterized constructor
If a constructor has some params to parameters
Example :
public class EgConstructor{
public static void main(String args[])
{
System.out.println("Inside the constructor class Eg1");
EgConstructor object = new EgConstructor(10,20);
EgConstructor object1 = new EgConstructor("AbdulKalam",83,"Male");
EgConstructor object2 = new EgConstructor('I','N','D','I','A');
}
EgConstructor(int a,int b)
{
System.out.println("Inside constructor 1");
int c = a+b;
System.out.println("the value of a = " +a);
System.out.println("the value of b = " +b);
System.out.println("the value of c = " +c);
}
EgConstructor(String name,int age,String sex)
{
System.out.println("Inside constructor 2");
System.out.println("Name = " +name);
System.out.println("Age = " +age);
System.out.println("Sex = " +sex);
}
EgConstructor(char a, char b, char c, char d,char e)
{
System.out.println("Inside constructor 3");
System.out.println(a+""+b+""+c+""+d+""+e);
}
}
Output :
Inside the constructor class Eg1
Inside constructor 1
the value of a = 10
the value of b = 20
the value of c = 30
Inside constructor 2
Name = AbdulKalam
Age = 83
Sex = Male
Inside constructor 3
INDIA