Debug with anonymous inner classes
来源:互联网 发布:好的教学软件 编辑:程序博客网 时间:2024/05/18 00:33
Tips 'N Tricks: Debug with anonymous inner classes
Use anonymous inner classes for easy debugging
SummaryBy Norbert Ehreke
Anonymous inner classes are mostly used for event handling in Java. However they can also prove useful for debugging purposes. This article explains how anonymous inner classes can be used for easier debugging. (400 words; January 2, 2006)
Printer-friendly version | Mail this to a friend
ow can we debug method calls that are not part of our own source code, say a call to Usually, when we derive a class, we can override existing methods by providing a new one: After instantiating buttons of type If we just want to roll our own visibility logic in one specific What happened here? The code between the curly braces declares the method In object-oriented terminology, When is overriding a method and creating an anonymous class on the fly useful? If you code with Swing, you have seen and coded such a mechanism before when you added an event listener, say an Anonymous inner classes prove helpful for debugging those classes where the source code is unavailable. Even when source code is available, setting a breakpoint in heavily used methods, such as JButton.setEnabled()
? Java provides us with anonymous inner classes, which come in quite handy for this problem. public class MyButton extends JButton {
public void setVisible( boolean visible ) {
// Rolling our own visibility
}
}
MyButton
, any call to setVisible()
will be handled by the code above. The problem is we do not want to declare a whole new class just to override one method, especially when the number of instantiations is limited. Anonymous inner classes let us override methods on the fly at the time of instantiation.JButton
, we just write the method as we declare the button:JButton myButton = new JButton() {
public void setVisible( boolean visible ) {
// Rolling our own visibility
}
};
setVisible()
and overrides the one from JButton
, but only for myButton
. We have not altered the JButton
class, we have not declared a new class, we have just given one special JButton
its own visibility logic. myButton
is an object of an unnamed, hence anonymous, class derived from JButton
.ActionListener
, to a GUI element. Now, imagine we have a big class with a bunch of buttons, where one button magically appears and disappears. We want to know why this problem occurs. Use the code above and set a breakpoint in the body of the setVisible
method. Then, as we run our program, the breakpoint will stop us just at the right place. With a look at the stack trace, we find the culprit that called setVisible()
unexpectedly and be able fix the problem. setVisible()
, might be cumbersome because we'll run into it for every instance of the class that implements setVisible()
. Anonymous inner classes allow surgical debugging for one specific instance.
Join the discussion about this article Click Here To Add Your Comment comment Anonymous 05/30/06 12:43 AM Not so much clear Anonymous 05/02/06 12:18 AM Debug with anonymous inner classes JavaWorld 12/31/05 02:20 PM
Printer-friendly version | Mail this to a friend
About the author
Norbert Ehreke is a senior development lead at Impetus Unternehmensberatung GmbH, a consulting company in Frankfurt, Germany. He is responsible for its framework development and has been involved in several Perl-, Java-, and C#-oriented projects. He studied at the Technical University (TU) in Berlin, Germany; the University of Maryland in College Park, Maryland; and at the Eidgenoessische Technische Hochschule in Zurich, Switzerland. He earned a master's degree in systems engineering from TU Berlin. His research interests include object-oriented programming (Java, Ruby, Perl, and C#). In his spare time he likes mountain biking, cooking, and the movies.
http://www.javaworld.com/javaworld/jw-02-2002/jw-0201-java101.html
http://www.javaworld.com/channel_content/jw-core-index.shtml
- Debug with anonymous inner classes
- Nested Classes,Static Classes,Non-static Classes,Inner Classes,Local Classes,Anonymous Classes
- 匿名内部类几个有意思的想法--Anonymous Inner Classes
- Anonymous Classes
- Inner Classes
- 匿名类Anonymous Classes
- Nested Classes和Inner Classes
- Java inner class and anonymous inner class
- Interfaces & Inner Classes
- Inner Classes Example
- Closures callbacks Inner Classes
- Java基础笔记 – 内部类 静态内部类 成员内部类 局部内部类 匿名内部类anonymous inner classes
- AS解决Ignoring InnerClasses attribute for an anonymous inner class that doesn't come with an associate
- Java中的Nested Classes和Inner Classes
- Threads and Anonymous Classes in JAVA
- anonymous classes(java as well as scala)
- 第八章: Interfaces & Inner Classes
- Context泄漏:Handlers & Inner Classes
- 如何按照HashTable动态设定类的属性和字段
- 经常口出鸟语,于是记录一下
- 【随想】关于【编译期确定与运行期确定】和【静态内存与动态内存】
- C#窗体运行的唯一性问题
- 国外房地产搜索引擎简评之一
- Debug with anonymous inner classes
- 睡睡睡
- BLL 和 DAL 之间
- Web Service开发的层次
- Hi
- 命令批处理实现对3389登录的日志记录
- 开山篇
- 从交流中学习
- 如何获取当前程序文件的路径 Current Path