These anonymous inner classes can access the static and instance variables of the enclosing outer class. They can also, believe it or not, access a snapshot of the local variables in the method that created the inner class object. To remind you this you are accessing an immutable snapshot, Java insists that the local variables you reference be final. When the class is created, a copy of all the local variables is bundled along with the object for all future references. So in a sense these classes act like closures, taking a little of the environment in which they were created along with them.
Instead of passing arguments to a constructor, your inner class methods can reach out and grab what they need directly from local variables in the enclosing method. The other technique is to use an instance initialiser block. You are only allowed one per anonymous inner class.
The requirement that the enclosing local variables that you access in your anonymous inner class be final is not quite as onerous as it first seems. Just create a new block and create new final local variables in it that copy the non-final ones of the outer block. You can also declare a local variable final inside a loop so long as the value does not change for the rest of the current iteration, even though it will change on the next iteration.
The inner class object may live beyond that of the method that invoked it, so you could not very well have the inner class modifying local variables in the caller that no longer exist. That is why you may only access final local variables. I repeat, you are not really accessing the local variables but a snapshot of them taken at the time the inner class object was instantiated. Anonymous classes are one of the bailing wire constructs in Java.
If you want to baffle those maintaining your code, wags have discovered javac.exe will permit anonymous classes inside static init code and static methods, even though the language spec says than anonymous classes are never static. These anonymous classes, of course, have no access to the instance fields of the object. I don’t recommend doing this. The feature could be pulled at any time.
The big drawback with anonymous classes is they can’t have explicit constructors. You can’t pass them any parameters when they are instantiated. You must rely on the somewhat goofy access to the frozen values of the final temporaries in the invoking method.
available on the web at:
optional Replicator mirror
Please email your feedback for publication, letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : . If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, especially when sending an ad-hominem attack, a rant composed mainly of obscenities or a death threat, please quote the offending passage and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. I can’t very well fix erroneous or ambiguous text if I can’t find it.
Your face IP:[220.127.116.11]
|Feedback||You are visitor number 103,271.|