Home > Back-end >  Why was an anonymous inner class to access the local variables of final modification must be used?
Why was an anonymous inner class to access the local variables of final modification must be used?

Time:03-08

Why was an anonymous inner class to access the local variables of final modification must be used?

CodePudding user response:

Say first conclusion: mainly in order to guarantee the consistency of the data,

Anonymous inner class can access the local variables, because at the bottom of the local variable values passed to the anonymous inner class, and exist in the form of an anonymous inner class member variables, the value transfer process was done by an anonymous inner class constructor

Local variables point to an object A, original, after creating the anonymous inner class member variables in an anonymous inner class also point to A object, but after A while the value of the local variable pointing to another object B, but at this time in an anonymous inner class object or point to the original A, go on running the program again could lead to the result of the program is running and expectations are different,
  • Related