Monday, 6 August 2018

Difference bet Constructor and Method in Java

Constructor Method
Constructor must have the same name as the class
name.
Method name can be same or can not be same
as the class name.
Constructor is used to initialize the variables. Method is used to show the behaviour of the
object and we can write business logic.
Constructor does not return any type. Method must return any type otherwise
It will be consider as constructor.
Constructor is called when we create the object.
Ex. A a = new A();
We need to called the method explicitly when
we require.
Ex. A a = new A();
a.showMessage();
If we don’t write constructor in Java class, then
Java compiler consider the default constructor.
In case of method, Java compiler don’t act like
constructor.

No comments:

Post a Comment