What is the difference between a constructor and a method?

Constructor is also a method/member function of the class. The following may make you feel different about constructor

1. Constructor has the same name as the class name.

class pikachu{

String name;
int level, HP;

pikachu(){

name = new String();
level = 2;
HP = 10;

}

}

2. The constructor is the first method that runs when an instance of the class (also called object) is created.

public static void main(String args[]){

pikachu james = new pikachu();

}

where,
james - an object (technically, identifier/handle) of class 'pikachu'
new - keyword to indicate allocation of memory
pikachu() - calling the constructor

3. The constructor, being the first method to run, is the usually used to instantiate objects and initialize variables, which are the members of the class.

name - string is instantiated
level,HP - initialized

4. Constructor returns no value and hence no return type.

Also,
1. Constructor, like any member function, can be overloaded.
2. Constructors can take all 4 access specifiers.


Deliver2inbox
 
A constructor is a member function of a class that is used to create instance or objects of a particular 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.
 
The important difference between constructors and methods are:

Constructors create and initialize objects that don't exist yet, while methods perform operations on objects that already exist.
Constructors can't be called directly; they are called implicitly when the new keyword creates an object. Methods can be called directly on an object that has already been created with new.
The definitions of constructors and methods look similar in code. They can take parameters, they can have modifiers (e.g. public), and they have method bodies in braces.
Constructors must be named with the same name as the class name. They can't return anything, even void (the object itself is the implicit return).
Methods must be declared to return something, although it can be void.
 
The difference is that PHP version 5.3.3 and above will treat function foo() as regular method and not constructor. Previous versions will treat it as a constructor. The difference is that calling a constructor by the same name of the class is deprecated. The first one is old php4 style "construct".
 
1. The first difference between method vs constructor in Java is that name of the constructor must be same with the name of the Class but there is no such requirement for a method in Java. methods can have any arbitrary name in Java.
2. The second difference between method and constructor in Java is that constructor doesn't have any return type but the method has the return type and returns something unless its void.
3. The third difference between method and constructor in Java is that special keyword this and super is used to call a constructor explicitly. no such thing as the method, they have their own name which can be used to call them.
 
Back
Top