as far as i know , the constructor return nothing , not even void ,
and also
return ;
inside any method means to return void .
so in my program
public class returnTest {
public static void main(String[] args) {
returnTest obj = new returnTest();
System.out.println("here1");
}
public returnTest ()
{
System.out.println("here2");
return ;
}
}
i am calling
return;
which will be returning VOID , but constructor is not supposed to return anything , the program compiles just fine .
please explain .
You do not specify a return type for a constructor. A return statement in the body of a constructor cannot have a return value.
Usually, constructors do not have a return statement. Their task is to write all necessary stuff into this , and it automatically becomes the result. But if there is a return statement, then the rule is simple: If return is called with an object, then the object is returned instead of this .
All Answers (1) By definition there is no possibility of returning a value from a constructor. A constructor does not support any return type.
Since constructor can only return the object to class, it's implicitly done by java runtime and we are not supposed to add a return type to it. If we add a return type to a constructor, then it will become a method of the class. This is the way java runtime distinguish between a normal method and a constructor.
return
in a constructor just jumps out of the constructor at the specified point. You might use it if you don't need to fully initialize the class in some circumstances.
e.g.
// A real life example
class MyDate
{
// Create a date structure from a day of the year (1..366)
MyDate(int dayOfTheYear, int year)
{
if (dayOfTheYear < 1 || dayOfTheYear > 366)
{
mDateValid = false;
return;
}
if (dayOfTheYear == 366 && !isLeapYear(year))
{
mDateValid = false;
return;
}
// Continue converting dayOfTheYear to a dd/mm.
// ...
return
can be used to leave the constructor immediately. One use case seems to be to create half-initialized objects.
One can argue whether that is a good idea. The problem IMHO is that the resulting objects are difficult to work with as they don't have any invariants that you can rely on.
In all examples that I have seen so far that used return
in a constructor, the code was problematic. Often the constructors were too big and contained too much business logic, making it difficult to test.
Another pattern that I have seen implemented controller logic in the constructor. If a redirect was necessary, the constructor stored the redirect and called returned. Beside that these constructors were also difficult to test, the major problem was that whenever to have to work with such an object, you have to pessimistically assume that it is not fully initialized.
Better keep all logic out of the constructors and aim for fully initialized, small objects. Then you rarely (if ever) will need to call return
in a constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With