Grade. Java-anonymous inner classes act like a “close”, meaning they “close” around the current local state. However, Java only allows these classes to close around finite variables. If this is not the case, the local state of the variable may change, but the version contained in the inner class will not, so it will refer to the "deprecated" instance. This may confuse the programmer.
Instead, Java requires that mutation be handled by the instance using methods, rather than a remapping variable. This leads to greater clarity and simplifies debugging. For more information on why Java does this, see this answer .
Since the class still contains a variable reference, the answer to your question is yes, this instance will not be garbage collected until the inner class relinquishes ownership of this variable.
source share