Methods vs Constructors in Java
The important difference between constructors and methods is that constructors initialize objects that are being created with the new
operator, 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
.
Other instructors and teaching assistants occasionally tell me that constructors are specialized methods. I always argue that in Java constructors are NOT specialized methods.
If constructors were methods at all, I would expect them to have the same abilities as methods. That they would at least be similar in more ways than they are different.
How are constructors different than methods? Let me count the ways...
Constructors must be invoked with the
new
operator while methods may not be invoked with thenew
operator. Related: Constructors may not be called by name while methods must be called by name.Constructors may not have a return type while methods must have a return type.
If a method has the same name as the class, it must have a return type. Otherwise, it is a constructor. The fact that you can have two MyClass() signatures in the same class definition which are treated differently should convince all that constructors and methods are different entities:
public class MyClass { public MyClass() { } // constructor public String MyClass() { return "MyClass() method"; } // method }
Constructors may initialize instance constants while methods may not.
Public and protected constructors are not inherited while public and protected methods are inherited.
Constructors may call the constructors of the super class or same class while methods may not call either super() or this().
So, what is similar about methods and constructors?
They both have parameter lists.
They both have blocks of code that will be executed when that block is either called directly (methods) or invoked via
new
(constructors).
As for constructors and methods having the same visibility modifiers... fields and methods have more visibility modifiers in common.
Constructors may be: private, protected, public.
Methods may be: private, protected, public, abstract, static, final, synchronized, native, strictfp.
Data fields may be: private, protected, public, static, final, transient, volatile.
In Conclusion
In Java, the form and function of constructors is significantly different than for methods. Thus, calling them specialized methods actually makes it harder for new programmers to learn the differences. They are much more different than similar and learning them as different entities is critical in Java.
I do recognize that Java is different than other languages in this regard, namely C++, where the concept of specialized methods originates and is supported by the language rules. But, in Java, constructors are not methods at all, much less specialized methods.
Even javadoc recognizes the differences between constructors and methods outweigh the similarities; and provides a separate section for constructors.
The main difference is
1.Constructor are used to initialize the state of object,where as method is expose the behaviour of object.
2.Constructor must not have return type where as method must have return type.
3.Constructor name same as the class name where as method may or may not the same class name.
4.Constructor invoke implicitly where as method invoke explicitly.
5.Constructor compiler provide default constructor where as method compiler does't provide.