What are the differences between constructor and method of a class in Java?

What are the differences between constructor and method of a class in Java?

Last updated:11/21/2021 10:41:12 PM

1 Answers

Ravi Vishwakarma
Ravi Vishwakarma


Constructor Method
Constructor is used for initializing the object state. 

Method is used for exposing the object's behavior.
Constructor has no return type. Method should have a return type.  Even if it does not return anything, return type is void.
Constructor gets invoked implicitly.  Method has to be invoked on the object explicitly.
If the constructor is not defined, then a default constructor is provided by the java compiler.  If a method is not defined, then the compiler does not provide it.
The constructor name should be equal to the class name.  The name of the method can have any name or have a class name too.
Final variable instantiations are possible inside a constructor and the scope of this applies to the whole class and its objects.  A final variable if initialised inside a method ensures that the variable cant be changed only within the scope of that method.


Answer