This question is really confusing
A ________ is a special method that has the same name as the class and is invoked automatically whenever an object of the class is instantiated.
Answers:
-
constructor
-
setter
-
getter
-
static method
I was thinking constructor is the only with the same name as the class, but wait! constructor is not really a method, it differs from method.
So i read this article and came to a conclusion that this question is wrongly formatted, am I right?
Constructorsare in fact a special method, that are used to initialize the state of the newly created instance. When you create an instance like:-Then, the instance of class A is created using
newkeyword, and then the constructorA()is invoked on that newly created instance.Further from that article that says: –
No this is wrong. Constructor don’t create instance, its the
newkeyword that does it. And then constructor initializes the state of the instance created as I stated above.From
JLS - Section 8.8: –Also from oracle tutorial