Search code examples
javacompiler-errorsunreachable-statement

How is this an "unreachable statement"?


I am making a login page, and i would like it if "Successfull login" appears in the console if the username and password match the database. So i simply added a System.out.println(); in the if statement, but it gets the error "unreachable statement". Why is that? Here is the loop:

if (user.equalsIgnoreCase(userFromDB) && hashedPass.equals(passFromDB)) {
        return "Correct username and password!";
        System.out.println("Login successfull using username \"" + user + "\"");
    }

Solution

  • This is not a loop, it is a conditional statement and even if it would be a loop it woudln't change a thing.

    This is an unreachable statement because return is the place where you get out of the method and return a value of an expression that is next to the return key word.