Home > DeveloperSection > Interviews > What is the difference between constructor and method?

Posted on    October-25-2010 12:00 AM

 Java Java 
 1 Answer(s)
  802  View(s)
Rate this:

Pushpendra Singh

Total Post:241

Posted on    November-08-2010 1:00 AM

A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.

A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.
Constructor does not have a return type.
Name of constructor is the same as that of class in which it is declared.

public class ClassA {

public ClassA()
// Initilize code in the constructor.

Don't want to miss updates? Please click the below button!

Follow MindStick