Constructors
are in fact a special method, that are used to initialize the state of the newly created instance. When you create an instance like:-
A obj= new A();
Then, the instance of class A is created using new
keyword, and then the constructor A()
is invoked on that newly created instance.
Further from that article that says: -
Constructors have one purpose in life: to create an instance of a
class.
No this is wrong. Constructor don't create instance, its the new
keyword that does it. And then constructor initializes the state of the instance created as I stated above.
From JLS - Section 8.8
: -
Constructors are invoked by class instance creation expressions
(§15.9), by the conversions and concatenations caused by the string
concatenation operator + (§15.18.1), and by explicit constructor
invocations from other constructors (§8.8.7).
Constructors are never invoked by method invocation expressions
(§15.12).
Also from oracle tutorial
Point originOne = new Point(23, 94);
The above statement has three parts (discussed in detail below):
Declaration: The code set in bold are all variable declarations that
associate a variable name with an object type.
Instantiation: The new keyword is a Java operator that creates
the object.
Initialization: The new operator is followed by a call to a constructor,
which initializes the new object.