I am using Java 8 Update 20 32 bit, Maven 3.2.3, Eclipse Luna Build id: 20140612-0600 32 bit.
After running lambdas, some classes in my projects started reporting compilation errors in maven ( mvn compile ).
These errors only appear when I use lambdas. If I go back to anonymous classes, the errors go away.
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 will compile it as follows:
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 believes that the final panel variable is called before it has been created), the variable will not be called until the button issues an action, and how can we When you start to act, the code must compile. In fact, it compiles as if I were not using 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 more strange things related to this problem:
- Eclipse does not report this error when automatically compiling a class. Eclipse uses the same JDK as maven to compile the class.
- If I use maven to compile the class using anonymous classes, I change the class to use lambdas and compile it using maven again, it does not report an error. In this case, it again reports an error if I use
mvn clean and then mvn compile .
Can someone help me solve this problem? Or try to reproduce this problem?
source share