Duplicate :
|
|
Relates :
|
|
Relates :
|
A reference to a method declared in a package-access class (via a public subtype) compiles to a lambda bridge; the qualifying type in the bridge method is the declaring class, not the referenced class. This leads to an IllegalAccessError. --- package p1; class A { public static void m() { System.out.println("A.m"); } } --- package p1; public class B extends A { } --- package p2; public class Test { public static void main(String... args) { p1.B.m(); // ok Runnable r = p1.B::m; r.run(); // runtime error } } --- $ -> java p2.Test A.m Exception in thread "main" java.lang.IllegalAccessError: tried to access class p1.A from class p2.Test at p2.Test.lambda$MR$main$m$64fce12b$1(Test.java:7) at p2.Test$$Lambda$1/1160460865.run(Unknown Source) at p2.Test.main(Test.java:8) $ -> javap -verbose -private p2.Test ... public static void main(java.lang.String...); descriptor: ([Ljava/lang/String;)V flags: ACC_PUBLIC, ACC_STATIC, ACC_VARARGS Code: stack=1, locals=2, args_size=1 0: invokestatic #2 // Method p1/B.m:()V 3: invokedynamic #3, 0 // InvokeDynamic #0:run:()Ljava/lang/Runnable; 8: astore_1 9: aload_1 10: invokeinterface #4, 1 // InterfaceMethod java/lang/Runnable.run:()V 15: return ... private static void lambda$main$0(); descriptor: ()V flags: ACC_PRIVATE, ACC_STATIC, ACC_SYNTHETIC Code: stack=0, locals=0, args_size=0 0: invokestatic #5 // Method p1/A.m:()V 3: return
|