John Assymptoth
John Assymptoth

Reputation: 8507

In Java, where can a return statement appear?

Besides inside a method/constructor body?

Also, is there something that can be returned by a constructor? Or can I only use "return;" without any Expression following it?

Upvotes: 3

Views: 2517

Answers (6)

Sven Ludwig
Sven Ludwig

Reputation: 1

I prefer to have only one return statement in a method and no return statement in a constructor. One more level of indentation is often a trivial thing to accept in comparison for what you get out of this best practice. It often makes the code much more readable. If I have a local variable for the value that is returned in the end, it often gives me the possibility to declare that variable as final, so that I can be absolutely sure that the variable is assigned exactly once. Often this gives you some insight into the actual complexity of your algorithm. However, I do make use of having several throws statements within a method. Also I prefer to have a Design by Contract with pre-conditions being checked in the beginning of the method and stated in the javadoc.

Upvotes: 0

Bert F
Bert F

Reputation: 87533

In Java, where can a return statement appear? Besides inside a method/constructor body?

Only in a constructor or method:

JLS - http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#6767

A return statement returns control to the invoker of a method (§8.4, §15.12) or constructor (§8.8, §15.9).


Also, is there something that can be returned by a constructor?

No

JLS - http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#6767

A return statement with an Expression must be contained in a method declaration that is declared to return a value (§8.4) or a compile-time error occurs.


Or can I only use "return;" without any Expression following it?

Yes

JLS - http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#6767

A return statement with no Expression must be contained in the body of a method that is declared, using the keyword void, not to return any value (§8.4), or in the body of a constructor (§8.8)

Upvotes: 7

Jon Skeet
Jon Skeet

Reputation: 1500145

The source of truth for this and so many other language lawyer questions is the Java Language Specification, in this case section 14.17:

A return statement returns control to the invoker of a method (§8.4, §15.12) or constructor (§8.8, §15.9).

ReturnStatement:
   return Expressionopt;

A return statement with no Expression must be contained in the body of a method that is declared, using the keyword void, not to return any value (§8.4), or in the body of a constructor (§8.8). A compile-time error occurs if a return statement appears within an instance initializer or a static initializer (§8.7). A return statement with no Expression attempts to transfer control to the invoker of the method or constructor that contains it.

To be precise, a return statement with no Expression always completes abruptly, the reason being a return with no value.

Note that this explicitly calls out that you can use a return statement in a constructor - but it does limit it to constructors and methods.

Upvotes: 5

anon
anon

Reputation:

You can use return without a value in a method with return type void. In other methods this isn't possible.

For example

    public void test(String s) {
       if (s.equals("")) {
          return;
       }
    }

A constructor shouldn't have a return statement inside its body since a Constructor has no return type by definition.

EDIT: As Jon Skeet pointed it out, constructors can have return statements with no values, but in most cases this isn't used.

Upvotes: 0

pseudoramble
pseudoramble

Reputation: 2561

I'm not sure about exactly where return statements can be used (or exactly what the specification says). They're most commonly used within methods to return values (or to end methods with no return value).

Constructors cannot return any value. Calling a constructor will have the VM allocate memory for the size of the object you're creating and then call upon the appropriate constructor within the object, which is a different process than the process of calling a method. The return statement may be able to be used, but no value can be returned from the constructor itself.

To "return" information from a constructor (more appropriately, indicate something is wrong during construction) exceptions can be used instead.

Upvotes: 0

AlexR
AlexR

Reputation: 115328

return statement can appear everywhere into method or constructor.

return should be followed by value compatible with return type of method. If method is void return does not return anything.

Constructors do not have return type at all (by definition). Therefore if you write return statement into constructor it should not be followed by any value.

Upvotes: 4

Related Questions