Reason for the exception java.lang.VerifyError: Bad type on operand stack

The problem arises because your lambda expression does not reference this or a member of this but a member of the outer this. Had you written class B like

public class B extends A {
    int innerData;
    public B() {
        super((data1, type) -> innerData = 1);
    }
}

the compiler rejected it without any doubts as accessing innerData implies accessing this.

The point about the outer instance is that it is a constant which is even available when the inner instance has not been fully constructed yet. So it’s correct to accept the code but unfortunately the compiler generates code which attempts to access the outer instance via an implicit field of the inner class instance, thus the lambda expression requires an instance of the inner class and attempting to use the not fully constructed inner class instance produces the error.

It can be easily demonstrated that the code can be compiled correctly:

public class B extends A {
    public B() {
        this(TestJavaCodes.this);
    }
    private B(TestJavaCodes outer) {
        super((data1, type) -> outer.parentData = 1);
    }
}

with that small change, the lambda expression refers to the outer instance without accessing the inner instance and no error arises.

Leave a Comment