-2

I've searched here for the question but could only found situations in which the person already has a variable with the value "false" or "true". In this case, I have a String variable called "Deactivated", and im trying to convert it to a "False" string, and then to a "False" bool. But I get the "unreachable code" error (Eclipse), even though im instantiating the DTO at the method's declaration... Here's the code:

public class TheController {
    public String getData(TheDTO theDTO) {    

        return "Just returning the variable for debugging reasons: " + theDTO.information; // returns the information without issue

        // converting it to String "False", and then to Bool.
        if (theDTO.information == "Deactivated") { // At this line is where the "Unreachable code" error appears.
            theDTO.information = "False";
        // Boolean.parseBoolean(theDTO.information)
        }
    }
}
  • 1
    Unreachable code means that in in your function returns a value before it get the chance to execute the next lines of code – Progs Feb 11 '20 at 20:13
  • 6
    there's no life after `return`: we leave the method – Dmitry Bychenko Feb 11 '20 at 20:13
  • 2
    What do you think `return` does? What happens to flow of control? Also take a look at [How do I compare strings in Java?](https://stackoverflow.com/q/513832) to find answer for one of your next problems. – Pshemo Feb 11 '20 at 20:24
  • 1
    Possible duplicate?: [Differences between System.out.println() and return in Java](https://stackoverflow.com/q/25456472) – Pshemo Feb 11 '20 at 20:29

1 Answers1

0

If you want to get a variable for debugging reasons, use System.out.println(). Return causes the entire method to cancel.

  • Can you clarify "Return causes the *entire method to cancel*"? If I would be at start of learning Java and have code like `void foo(){doSomethig(); return;}` then by `entire method to cancel` I would assume you mean that `doSomethig()` would also be canceled (which is not true). – Pshemo Feb 11 '20 at 20:28
  • Yeah I should have used "ends". If you would put doSomething() after return, you would get the unreachable code error as well. Return basically says "All right, this method is finished, and this is its result". Every method that has a return value (the one you write before the method name) needs to return a value at some point. [link](https://www.w3schools.com/java/ref_keyword_return.asp) –  Feb 11 '20 at 20:33
  • 1
    Feel free to [edit] your answer then to improve its clarity :) – Pshemo Feb 11 '20 at 20:39