I'm using Java 8 Update 20 32 bits, Maven 3.2.3, Eclipse Luna Build id: 20140612-0600 32 bits.
After starting using lambdas, some classes in my projects started to report compilation errors in maven (mvn compile).
These errors appears only when I use lambdas. If I switch back to anonymous classes, the errors are gone.
I can reproduce the error with a simple test case:
package br;
import java.awt.Button;
import java.awt.Panel;
public class Test {
private final Button button;
private final Panel panel;
public Test() {
button = new Button();
button.addActionListener(event -> {
System.out.println(panel);
});
panel = new Panel();
}
}
I compile it this way:
mvn clean;mvn compile
And I get this error:
[ERROR] /C:/Users/fabiano/workspace-luna/Test/src/main/java/br/Test.java:[14,44] variable panel might not have been initialized
Although the error message is pretty clear about what is happening (the compiler thinks the final variable panel is being called before it is instantiated), the variable will not be called until the button generates an action, and how we can't say when the action will happen, the code should compile. Indeed, it compiles as it should if I don't use lambdas:
package br;
import java.awt.Button;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test {
private final Button button;
private final Panel panel;
public Test() {
button = new Button();
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(panel);
}
});
panel = new Panel();
}
}
I noticed two other strange things related to this problem:
- Eclipse don't report this error when it auto-compile the class. Eclipse is using the same JDK as maven to compile the class.
- If I use maven to compile the class using anonymous classes, then I change the class to use lambdas and compile it using maven again, it doesn't report the error. In this case it just reports the error again if I use
mvn cleanfollowed bymvn compile.
Can someone help me to fix this problem? Or try to reproduce this problem?