1

This program run successfully . What is use of blank return in constructor as we know it return this implicitly . Is it is a bug in java or have some use .

class Demo
{
    int salary;

    Demo()
    {
        salary =11500;
        //return this;(implicitly)
        return;
    }
    public static void main(String... s)
    {
        System.out.println(new Demo().salary);
    }
}   
| improve this question | | | | |
  • 3
    Perhaps to be explicit. Who knows? Looks about as useful as if(something == true) to me. Note that, just because someone wrote it, doesn't necessarily mean that it's good. – Robert Harvey Sep 8 '16 at 14:12
  • Constructors shouldn't do real work. However, this is why they should be allowed to use return: stackoverflow.com/a/733858/1493294 – candied_orange Sep 8 '16 at 14:22
  • There are valid reasons to return early in a constructor. Yes, they shouldn't do much work, but sometimes it is the cleanest solution. Every rule has at least one exception, including "no GOTOs" and "never use ternary 'if' expressions" and "constructors shouldn't do much." – user22815 Sep 8 '16 at 14:30
5

Return in a void function is used to escape the control flow before the natural end of a function. As such, it needs to be a valid statement.

Going out of your way to explicitly forbid it as the final statement in a function serves no purpose except to break existing code.

You may want to lint for it, but you cannot make it an error or ill-formed.

| improve this answer | | | | |
  • 1
    Exactly. And many compilers will provide a convenient warning that code past the return statement is unreachable. – sleblanc Jan 27 '17 at 19:01
2

Putting a single return at the end of the constructor doesn't change anything but it isn't harmful and is no different than putting a return at the end of a void method. However, you put a return at another point in the constructor like in a guard condition, it very much does make a difference. Since this is allowable, the value of restrict the use of return to only those situations where it isn't redundant isn't really worth the effort. It would complicate the language for no good reason.

| improve this answer | | | | |
0

For the caller a constructor behaves similar to a static method that returns an instance. But from the inside a constructor behaves like a void returning instance method, so not being able to specify a value on the return statement is only consistent.

You could mentally model it as new XYZ() first creating an instance initialized to zero in compiler-generated-code and then calling the user-written constructor on that instance, just like a normal instance method.

Something like:

public static T create() // Compiler generated
{
    T instance = AllocateZeroed<T>();

    instance.field1 = ABC; // field initializers
    instance.field2 = DEF; // field initializers        
    ...

    instance.constructor(); // user-written constructor chain

    return instance;
}
| improve this answer | | | | |
  • If you are used to Ruby, that is pretty much exactly what happens: new is an instance method of the Class class (which, in other words, makes it a "class method"). new first calls allocate (which is also an instance method of the Class, thus a "class method"), which creates a new empty instance of self (i.e. the class it was called upon), then it calls initialize (which is an instance method in the class) on that newly allocated empty object. – Jörg W Mittag Sep 8 '16 at 15:12
  • Objective-C works the same way, but even more obvious, because it lacks the new convenience helper method, and you usually call [[Foo alloc] init] manually. – Jörg W Mittag Sep 8 '16 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.