0

How to use the return to retrieve a value out of an if statement? I don't want to return immediately, just like that:

if(){
return "something";
}

This isn't working for me, because if the return is successful the method returns, but I need to return it and when the return is complete to continue the actions in the method.

7
  • You should only ever have one exit point for a function. IE, I would not recommend putting a return in a if statement. Commented May 10, 2011 at 17:10
  • 1
    This question should get a prize for having so many instant duplicate answers :) Commented May 10, 2011 at 17:10
  • 6
    @user Not only is that highly debatable, it has nothing to do with this; he doesn't want to return Commented May 10, 2011 at 17:11
  • Hah , just forgot to tell you it is in try :) Commented May 10, 2011 at 17:11
  • 12
    @user489041 there's nothing wrong with multiple return statements. Having only one exit point is an out-dated pattern. Commented May 10, 2011 at 17:12

7 Answers 7

10

Try something like:

String result = null;

if(/*your test*/) {

    result = "something";

}

return result;
Sign up to request clarification or add additional context in comments.

1 Comment

forgot to ell that the if is in try
5

This should be the easiest way in your case

return yourCondition ? "ifTrue" : "ifFalse";

Comments

2

If you want to "return" values from a method without actually returning from the message, then you have to define setter methods on the calling class and call them, like this:

public class Caller {

     private boolean someState;

     // ...

     public void doSomething() {
         // the method call
         Worker w = new Worker(this);
         int result = w.workForMe();
     }

     public void setState(boolean state) {
         this.someState = state;
     }

}

And the Worker

public class Worker {

    private Caller caller;

    public Worker(Caller caller) {
        this.caller = caller;
    }

    public int workForMe() {
        // now the conditions:
        if(clearBlueSky) {
            // this emulates a "return"
            caller.setState(true);
        }
        // this returns from the method
        return 1;
    }

}

2 Comments

+0: This has to be by far the most complex solution. ;)
@downvoter - As far as I understood the question: he/she wanted to send intermediate results to the caller without actually leaving the method. Which does not work with return (there's no way back).
2

Store your string in a variable.

String s = null;
if( somecondition ) {
    s = "something";
}
// do other stuff
return s;

Comments

1

return is for methods. You probably want something like this:

int foo;

if (someCondition) {
    foo = 1;
} else {
    foo = 2;
}

Comments

0

Use a finally block or save the return value in a variable that you return at the end of your code.

1 Comment

@user639285, yes in a try statement, finally is designed for your specific case. Use a finally block.
0

Do you mean something like

String result = "unknown";
if(condition){
    result = "something";
}
// do something.
return result;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.