I read in many java books that constructor have no returns type , it means that it does not return anything ? is it really happen ? or it may return something ? I want to know the reason.
Please anyone give me the technical reason.
I read in many java books that constructor have no returns type , it means that it does not return anything ? is it really happen ? or it may return something ? I want to know the reason.
Please anyone give me the technical reason.
I read in many java books that constructor have no returns type , it means that it does not return anything ?
Yes, its return type is V
for void.
is it really happen ?
Yes.
or it may return something ?
No.
I want to know the reason.
The question shouldn't be; Why not?,
The question should be; Do I really have? and the answer is no.
it means that it does not return anything ?
No,It won't.
is it really happen ?
Yes It happens
or it may return something ?
Again No,it won't return anything.
Providing constructor is optional,the name it self indicating, it just helps in construction of the object.Wont return anything.
Constructors
have one purpose is to create an instance
of a class.Where as The purpose of methods, by contrast, is much more general. A method's basic function is to execute Java code.So Constructor
never returns anything where as method
may or may not return somthing.
While constructors technically return nothing, the new
call creates a new skeleton object at defaults, and then calls the correct constructor(and initializer blocks).
Implicitly it will return the object of the corresponding class. WE should not mention the return type for constructor
A constructor is a special block of code that is called whenever a new object is created. It is called to initialize the new object.
A constructor does not return anything, and you never call a constructor directly. You use the new
operator to create a new object. The new
operator reserves memory for the object, then calls the constructor to initialize it, and then returns a reference to the new object.
Constructors are for initialising objects. They do their work, and that's it - the object is initialised. Returning stuff is NOT what constructors are for.
Please refer to (Can constructor return a null object?)
It is a common behavior across all OOP languages that constructors do not have a erturn type in their signature. In fact, an instance of the class type enclosing the constructor definition is returned.