There is no way to hold the return value of constructor , because the constructor is called when the object is instantiated;
Eg.
<class name>obj=new <class name>(args.. of constructor)
myClass obj=new myClass();
Now in above situation before the assignment operator (=) i.e. at holding position a user defined type (your class type which have different set of method to dealt with its internal type) is placed. If you trying to hold any other type (e.g. primitive types are user defined type (class)) then it is illegal.
Let us take a simple example to illustrate it.
Suppose there is a hypothetical situation as:
Class demo { Private int num1; Peivate int num2; Public int Demo() { Return 100; } }
Now we create object of this class as:
Demo dm=new demo();// usual way to create
Int res;
Demo res=new demo()// hypothetical; based on situation
There are following reasons that causes the constructors never return any value as:
1. Firstly In hypothetical approach get fail, because the type, demo(which contains two integer as member as a single data type “demo”).on other hand type int as single data unit;
2. Second one demo type is creating reference that already referenced by type “int res”, which is absolutely wrong approach, and never possible.
3. Thirdly suppose that for a time movement, the approach was right. Then how we initialize the Demo object with single integer value return by the constructor, I think it is almost not possible ;
Thus the conclusion is that, it is not possible to be constructor with return type exist, by technically, syntactically are programmatically.
Thanks
Liked By
Write Answer
any one can tell me, why constructor have no return type?
Join MindStick Community
You have need login or register for voting of answers or question.
shreesh chandra shukla
31-Jul-2013HI Andrew!
There is no way to hold the return value of constructor , because the constructor is called when the object is instantiated;
Eg.
Now in above situation before the assignment operator (=) i.e. at holding position a user defined type (your class type which have different set of method to dealt with its internal type) is placed. If you trying to hold any other type (e.g. primitive types are user defined type (class)) then it is illegal.
Let us take a simple example to illustrate it.
Suppose there is a hypothetical situation as:
Now we create object of this class as:
There are following reasons that causes the constructors never return any value as:
1. Firstly In hypothetical approach get fail, because the type, demo(which contains two integer as member as a single data type “demo”).on other hand type int as single data unit;
2. Second one demo type is creating reference that already referenced by type “int res”, which is absolutely wrong approach, and never possible.
3. Thirdly suppose that for a time movement, the approach was right. Then how we initialize the Demo object with single integer value return by the constructor, I think it is almost not possible ;
Thus the conclusion is that, it is not possible to be constructor with return type exist, by technically, syntactically are programmatically.
Thanks