An anonymous class in a static method, contains a reference to what?

Afaik, in Java, an anonymous inner class always contains a reference to the enclosing instance of its outer class. Now, what happens when I put an anonymous class inside a static method? As there is no object of its outer class, does it contain a reference to the class that calls the static method? I am a bit confused. Consider the Android example below (using the parse.com framework):

public class OnlineQuery { public static void queryUsers(final ListenerInterface listener) { ParseQuery<ParseUser> query = User.getQuery(); query.findInBackground(new FindCallback<ParseUser>() { @Override public void done(final List<ParseUser> userList, ParseException e) { listener.reportBackToCallingActivity(); // to which class is as a reference held here? } }); } } public class MainActivity extends Activity implements OnlineQuery.ListenerInterface { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); OnlineQuery.queryUsers(this) } ... } 

Also, is a listener used, as shown in the bad practice example regarding memory leaks? Thanks!

+4
source share
2 answers

An instance of an anonymous inner class does not reference an outer class.

+1
source

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; /* Temporary program. */ 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; /* Temporary program. */ 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 #1 // Method java/lang/Object."<init>":()V 4: return public java.lang.Object get(); Code: 0: aconst_null 1: areturn } 

It seems that anonymous classes declared in static methods are just anonymous static classes.

0
source

All Articles