Use final for classes inside methods

reason

When accessing local variables inside a method from an inner class in a method, final must be used before jdk8. After decompilation, it can be seen that the inner class will actually generate a class with a constructor, and the referenced variables are passed in as parameters, including the outer class instance (this is why the inner class will hold the reference of the outer object by default, which is easy to cause memory leak), and then assign it to its own variable, that is to say, if the value of the external variable is changed in the inner class, what is actually changed is only the variable value of the inner class itself, not the value of the external variable, just like a method passing a variable over In the same way, you cannot change its value in the method to cause the external variable to change, and the variables in the method are local. So in order to prevent this inconsistency, it must be final.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325719247&siteId=291194637
Recommended