Constructor or method
I cannot figure out how the constructor differs from the method in Java. For me personally, the only difference is using the special word "this" in constructor. I would like to distinguish between their functions and structure. Help me understand, please.


Sachin SinghRank #66 · 26.2K reputationPosted Apr 7, 2021, 4:07 AM EDT
Rijwan AnsariRank #82 · 24.2K reputationPosted Apr 7, 2021, 8:30 AM EDT
Satya KarkiRank #78 · 24.6K reputationPosted Apr 7, 2021, 8:45 AM EDT
Salman BegRank #284 · 8.8K reputationPosted Apr 6, 2021, 12:57 PM EDT
Aman GuptaRank #193 · 12.4K reputationPosted Apr 6, 2021, 12:40 PM EDT
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.newoperator, while methods perform operations on objects that already exist.Constructors can't be called directly; they are called implicitly when the
newkeyword creates an object. Methods can be called directly on an object that has already been created withnew.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).