A constructor in Java is a block of code similar to a method that’s called when an
instance of an object is created.
Constructor will be called automatically when the object is created.
Constructor name must be similar to name of the class.
Constructor should not return any value even void also. Because basic aim is to
place the value in the object. (if we write the return type for the constructor then
that constructor will be treated as ordinary method).
Constructor definitions should not be static. Because constructors will be called
each and every time, whenever an object is creating.
It is basically used to initialize values for the object
In “Java” Constructors are basically divided into 3 types. They are
1. No argument Constructor
2. Argument Constructor
3. Copy Constructor
No Argument Constructor:-
A Constructor that has no Parameter or argument is called “No Argument
Constructor”.
Whenever we create an object “we does not pass any values from the object”
It is also used to initialize values for the object
It is Automatically called when we creates the object of class
In this Constructor the programmer must remember name of Constructor is
same as name of class
No argument Constructor never declared with the help of Return Type. Means
cant Declare a Constructor with the help of “void Return Type. “
It is called by the JVM automatically itself whenever the programmer creates an
“Object”.
Syntax:
Classname()
{
Initialize values
}
Ex:
Class addition
{
int a,b;
addition()
{
a=10;
b=20;
}
void display()
{
System.out.println(“Result “+(a+b));
}
}
Class student
{
Public static void main(String args[])
{
addition obj=new addition();
obj.display();
}
}
In the above example “addition” is class. When we create object
“obj” then the “JVM” automatically calls and initializes values in the constructor
“addition()”. This constructor does not contain any values at the time of object
creation because of this reason it is called “No argument Constructor”.
Argument Constructor:-
A Constructor that has Parameter or argument is called “No Argument
Constructor”.
Whenever we create an object “we pass values from the object”
It is also used to initialize values for the object
It is Automatically called when we creates the object of class
In this Constructor the programmer must remember name of Constructor is
same as name of class
Argument Constructor never declared with the help of Return Type. Means cant
Declare a Constructor with the help of “void Return Type. “
It is called by the JVM automatically itself whenever the programmer creates an
“Object”.
Syntax:
Classname(parameterlist)
{
Initialize values
}
Ex:
Class addition
{
int a,b;
addition(int x,int y)
{
a=x;
b=y;
}
void display()
{
System.out.println(“Result “+(a+b));
}
}
Class student
{
Public static void main(String args[])
{
addition obj=new addition(10,20);
obj.display();
}
}
In the above example “addition” is class. When we create object
“obj” then the “JVM” automatically calls and initializes values in the constructor
“addition(10,20)”. This constructor pass values 10,20 to the constructor in the
addition class. Here we pass parameters from the object that’s why it is called
“Argument Constructor”.
Copy Constructor:-
A Constructor that copies values from another object is called “Copy Constructor”
It Copies the values from either “No arugment Constructor” or “ Argument
Constructor”
Some times it copies values from both of them
Here we pass “Object” as a Paramter
It is also used to initialize values for the object
It is Automatically called when we creates the object of class
In this Constructor the programmer must remember name of Constructor is
same as name of class
Copy Constructor never declared with the help of Return Type. Means cant
Declare a Constructor with the help of “void Return Type. “
It is called by the JVM automatically itself whenever the programmer creates an
“Object”.
Syntax:
Classname(classname Object)
{
Initialize values
}
Ex:
Class addition
{
int a,b;
addition(int x,int y)//parameterized constructor
{
a=x;
b=y;
}
addition(addition dobj)//copy constructor
{
a=dobj.a;
b=dobj.b;
}
void display()
{
System.out.println(“Result “+(a+b));
}
}
Class student
{
Public static void main(String args[])
{
addition obj1=new addition(10,20);//parameterized constructor
addition obj2=new addition(obj1);//copy constructor
obj1.display();
obj2.display();
}
}
In the above example “addition” is class. When we create object
“obj1” then the “JVM” automatically calls and initializes values in the constructor
“addition(10,20)”. There after those values are copied by another object “obj2” with the
help of “copy Constructor” that is declared at “class addition”.
There is no destructor in “Java”. Because “Java” has a feature called “Automatic
Garbage Collection”.
Some times the programmer calls “protected void finalize()” for the sake of
garbage issue.