I am messing around a little bit with Java and the return
statement of a void-method
.
In each of the methods, the compiler within my Eclipse IDE (Eclipse Mars 4.5.2, Java 7) has a different output. I've written the warnings/the error as comments over the regarding line:
public static void foo() {
System.out.println("foo() - 1");
boolean b = true;
if (b)
return;
System.out.println("foo() - 2");
}
public static void foo2() {
System.out.println("foo2() - 1");
// compiler warning: "The value of the local variable b is not used"
boolean b;
if (b = true)
return;
System.out.println("foo2() - 2");
}
public static void foo3() {
System.out.println("foo3() - 1");
if (true) {
return;
}
// compiler warning: "Dead code"
System.out.println("foo3() - 2");
}
public static void foo4() {
System.out.println("foo4() - 1");
return;
// compiler error: "Unreachable code"
System.out.println("foo4() - 2");
}
Each method has the same behaviour. The if-statement
is true and the return-statement
is invoked. The last System.out.println(..)
isn't invoked any more. But why does the compiler outputs different things?
Thanks for your help!
Kind regards
P.S.: My Eclipse looks like that:
P.P.S.: if I compile this file via javac Test.java
I don't get any warnings, although I have to disable them manually by using -nowarn
(javac documentation), but I did not do that: