Garbage collection when final variables used in anonymous classes
问题 If I have code similar to the following: public Constructor(final Object o) { taskSystem.add(new CycleTask(15, 15, -1) { @Override public void execute() throws Throwable { //access o here every 15 cycles forever } }); } When would o be garbage collected, if ever? Only when the task has been collected, or will it remain in memory forever because it's final? 回答1: o might get garbage collected once it is not reachable any longer, whether it is final or not. Obviously, as long as execute is