anonymous classes
A type of inner class that has no name that you define right in the middle of a method (where static init blocks and instance init blocks count as methods). You define it, and create an object of that type as a parameter all in one line. Used for creating simple delegate callback objects. The syntax is strange. It does not use the keywords class , implements or extends . You can refer to this of the outer class via MyOuterClass . this . You can refer to the outer class’s methods by MyOuterClass . this . myOuterInstanceMethod (), MyOuterClass . myOuterInstanceMethod () or simply myOuterInstanceMethod () if there is no ambiguity. Anonymous inner classes are often used in setting up listeners.
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.
本文深入探讨了Java中匿名内部类的概念及其使用场景。解释了如何在方法中定义匿名内部类,并创建该类型的对象。讨论了匿名内部类可以访问外部类的静态和实例变量,以及在创建内部类对象时对外部方法局部变量的快照访问。此外,还介绍了为维护代码而访问局部变量时的final限制,以及匿名内部类的一些典型应用。
274

被折叠的 条评论
为什么被折叠?



