Showing posts with label Java methods. Show all posts
Showing posts with label Java methods. Show all posts

Tuesday, 11 August 2015

Difference between Methods and Constructors in Java

Methods Vs Constructors in Java

What is the difference between Methods and Constructors in Java?


               It is obvious that Constructor and Method in Java make confusion for the beginners. Here are few points which illustrate the difference between a constructor and method in Java.


  No.

Methods

Constructors


   1

It is not necessary to use the same name of the class to create a Method.


The name of the Constructor should be same as the class name it resides.

   2

Method is an ordinary member function which is used to expose  the behavior of an object


Constructor is a member function of a class used to initialize the state of an object

   3

Methods must have return type unless it is specified as void.


Constructor does not have a return type.

   4

Compiler does not create a method in any case if one is not available.


Java compiler creates default constructor if the program doesn’t have one.

   5

Methods invoked explicitly. 
Invoked using the dot operator.


Constructor invoked implicitly. i. e. Invoked using the keyword ‘new’.


               Hoping that the above table gives an idea about how Constructors are different from methods in Java.

Constructor Method Difference