Having a class definition inside a method is just syntax: it is still a perfectly normal class definition.
For the Inner objects ( new Inner() ) that you create, this means:
- each object will have the right to garbage collection, like any other object, immediately after the loop iteration
- yes, the object will eventually be garbage collected
- the object will be delayed until garbage is collected, but it will not be accessible from other places (since the link to it did not leak).
For the class itself, this means:
- the class will be loaded as usual (only once)
- the class will not be reloaded at each iteration
- the class will not be reloaded on the second
test call - the class can be GCed according to the usual rules of the GCing classes (which are pretty tight)
source share