Search code examples
javakotlinjvmlazy-evaluation

NoClassDefFoundError when use kotlin Lazy with inner class


Just consider code below:

fun doAction(data: Any?, action: (Any?) -> Unit) {
    action(data)
}

class Outer {
    val a = "a"
    val b = "b"
    val inner by lazy { Inner() }

    inner class Inner {
        init {
            doAction(a) { println(it) }
            doAction(b) { println(it) }
        }
    }
}

fun main() {
    Outer().inner
}

I expect get result, print "a" and "b", but only print "a" and an error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/example/Outer$Inner$2 (wrong name: org/example/Outer$inner$2)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)

kotlin version: 1.3.72


Solution

  • It's a variable declare problem

    When I change the val inner to val temp, it won't get error