. Advertisement .
..3..
. Advertisement .
..4..
Today I still get the ”error: java.lang.verifyerror: bad type on operand stack” error when running my program.
public class TestJavaCodes {
int parentData = 0;
public void init() {
A ob = new B();
}
public static void main(String[] args) {
TestJavaCodes testJavaCodes = new TestJavaCodes();
testJavaCodes.init();
}
public static class A {
public A(MyLambdaFunc lambdaFunc) {
}
}
public class B extends A {
public B() {
super((data1, type) -> {
parentData = 1;
});
}
}
@FunctionalInterface
public static interface MyLambdaFunc {
public void onData(String data, int type);
}
}
I don’t know what the reason is and the way to fix it? Can you help me?
The cause: The issue arises because your lambda expression refers to a member of the outer
this
rather thanthis
or a member ofthis
. Had you written classB
likethe compiler rejected it since accessing
innerData
implies accessingthis
.Solution: The lambda expression refers to the outer instance without accessing the inner instance with the small change below, and no error occurs.