Return an anonymous class that uses the final primitive. How it works?

I was wondering if anyone could explain how the following code works:

public interface Result { public int getCount(); public List<Thing> getThings(); } class SomeClass { ... public Result getThingResult() { final List<Thing> things = .. populated from something. final int count = 5; return new Result { @Override public int getCount() { return count; } @Override public List<Thing> getThings(); return things; } } } ... } 

Where is the primitive int, list, and instance of List stored in memory? It can't be on the stack .. so where? Is there any difference in how links and primitives are handled in this situation?

Thanks Tim P.

+4
source share
1 answer

The final locales used (and any external this links) are copied to the synthetic fields of the inner class at build time. Links and primitives, as always, are treated the same. Both copied (small).

You can use javap from the JDK to find out what is generated.

+4
source

Source: https://habr.com/ru/post/1307785/


All Articles