I made a small throwaway class (this is Java 9, but I doubt it matters) and used javap to disassemble it, and apparently they do not explicitly declare a field containing a reference to an external class, unlike anonymous classes in instance methods.
Here is the source code:
import java.util.function.Supplier; public class Temp { static <T> Supplier<T> refSupplier(T obj) { return new Supplier<>() { public T get() { return null; } }; } public static void main(String... args) {} }
And here is the disassembled class file for the anonymous Supplier :
PS C:\Users\Sylvaenn\OneDrive\Documents\Programs\Java\src> javap -c -p Temp`$1 Compiled from "Temp.java" class Temp$1 implements java.util.function.Supplier<T> { Temp$1(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public T get(); Code: 0: aconst_null 1: areturn }
Here is the source code for Temp , with a static class replacing the anonymous class:
import java.util.function.Supplier; public class Temp { static class UselessSupplier implements Supplier<Object> { @Override public Object get() { return null; } } public static void main(String... args) {} }
And here is his bytecode:
PS C:\Users\Sylvaenn\OneDrive\Documents\Programs\Java\src> javap -c -p Temp$`UselessSupplier Compiled from "Temp.java" class Temp$UselessSupplier implements java.util.function.Supplier<java.lang.Object> { Temp$UselessSupplier(); Code: 0: aload_0 1: invokespecial
It seems that anonymous classes declared in static methods are just anonymous static classes.
source share