Is a constructor in Java a method? Is it a special method?
Why? What makes a constructor a method or not a method?
There are examples where it is said that it is a method, such as Constructor in Java :
A constructor is a special method of a class that is automatically called whenever an object of that class is declared.
And examples where the opposite is said as in 12 Rules of Overriding in Java You Should Know :
Because constructors are not methods
...
I'm asking if a constructor is a method.
I'm not asking what a constructor is or what a method is. Nor what are their differences or similarities. These questions may be relevant to whether or not a constructor is a method, but they are not the primary subject of the question.
It seems but it is not
In Java, a constructor looks like a method, but it really isn't.
Why? What makes a constructor not a method?
Very simple, since you don't want them to talk to you about differences: JAVA has decided so, that's why in its documentation it always talks about constructors or methods .
Also, by sketching a definition (inevitably based on differences), as its name implies, a constructor has the ability to construct an instance of a class. A method does not build, it does not create, it only obeys the order that has been given to it.
Java documentation always makes a difference:
Also:
Here is an interesting article (in English).
For example, it says, among other things, that in the same class you can have a constructor (this always bears the name of the class) and a method with the same name... the two will work differently, each one doing the function for which exists.
In short, a constructor looks like a method, but it is just a constructor, because JAVA wants it to be. :)
Some illuminating differences:
The important difference between constructors and methods is that constructors create and initialize objects that do not yet exist, while methods perform operations on objects that already exist.
Constructors cannot be called directly. They are called implicitly when the keyword
new
creates an object. Methods can be called directly on an object that has already been created withnew
.Constructor and method definitions look similar in code. They can take parameters, they can have modifiers (for example, public), and they have method bodies.
Constructors must be named with the same name as the class name. They can't return anything, not even void (the object itself is the implicit return).
Methods must be declared to return something, and when they don't return anything it has to be indicated using
void
.NO
A constructor in Java is not a method and it is not a special method either. It looks like a method without Result, but it is not a method.
The Java Language Specification is the Java language reference document.
We can read in 8.1.6. Class Body and Member Declarations
We can read in 8.8. Constructor Declarations
If a constructor were a method then a constructor would be a member. And the JLS clearly says no, a constructor is not a member, so it can't be a method.
YES and NO
In the sense of the word method in Spanish, the constructor is.
Technically and formally it is not, at least not in Java.
It is very common to explain (especially to OOP newbies) what a constructor is, comparing it to methods. I find it perfectly valid, it is unproductive to get too dogmatic. But if I explain it like this I also have to mention that there are important differences:
static
orfinal
(it doesn't make sense).new
that returns a new instance of the class in question, or assuper(...)
calling a constructor of the superclass within a constructor.Apart from that it has many aspects in common with a method
private
,protected
orpublic
.In short: Looking at the constructor as a method (aware that it is not) does not put the sanity of an engineer in danger, while not losing awareness that there are important differences as stated above.
Dogmatically insisting that "it is not a method" without referring to the importance of the differences is as useful and pedagogical as saying to a child "the old man from Christmas is a lie, the postman brought the gifts."
In order not to start a pointless debate I want to base my answer on what the official documentation describes .
A constructor is similar to a method but is not the same due to the differences mentioned.
For example, the Car class has a constructor:
Automovil
To create a new named objectmiAutomovil
, a constructor is called by the new operator:new Automovil(30, 0, 8)
creates memory space for the object and initializes its fields.Although
Automovil
it has only one constructor, it could have others, including a no-argument constructor:Automovil miAutomovil = new Automovil();
Calls the constructor with no arguments to create a new objectAutomovil
namedmiAutomovil
.Constructors in Java are a special type of method which matches the name of the class and has no return type, when the new keyword is written, Java reserves space in memory to store the object, it also takes care of looking for the constructor, constructors are often used to initialize the instance variables of the Class
PS: They are methods since they receive parameters and have logic inside their body, but they are a special type of method which is only accessed when the new keyword is used
Source : Oracle Java Certified Associate (JAVA OCA 8 ).