TIPS: use anonymous classes to simplify debugging

Source: Internet
Author: User

How can we debug method calls that are not their own source code? For example, call jbutton. setenable. The anonymous class provided by Java can solve this problem well.

Generally, when we inherit a class, we can provide new methods to overwrite the existing methods in the class:

public class MyButton extends JButton {   public void setVisible( boolean visible ) {   // Rolling our own visibility    }}

After the instantiate mybutton class, any call to the method setvisible () will call the setvisible () method in the above Code. The problem is that we don't want to inherit the entire class just to override a method, especially when the required instance (instantiation) is very limited. Anonymous classes allow us to override methods while instantiating them.

If we only want to add our own visual logic to a jbutton object, we can rewrite this method while declaring this button object:

JButton myButton = new JButton() {   public void setVisible( boolean visible ) {   // Rolling our own visibility    }};

What has this code done? The code in the braces ({}) declares the setvisible () method and overwrites the one in the jbutton class, but this is limited to the mybutton object. We didn't change the jbutton class or declare a new class. We only gave a special jbutton object its own visual logic. In object-oriented terms, mybutton is an anonymous, Class Object inherited from the jbutton class.

When is this technology used to create anonymous classes and override methods at the same time? Suppose you are writing a swing program. Before you add an event listener (called actionlistener) to a GUI object (element), you have compiled code for this mechanism. Now, let's assume that we have a huge class with many buttons in it, but a button is instantly present. You want to know why. Use the above Code and set the breakpoint on the setvisible () method. Then, when you run your program, the breakpoint you set will pause the program in the appropriate place. Check the stack trace and we will find that the setvisible () method is not called as expected and fix it.

Anonymous classes are useful when debugging is similar to the class that is not available in source code. Even when the source code is available, it is very troublesome to set breakpoints on a lot of methods (such as setvisible), because we implement setvisible () in every method () the Class Object of the method must be transferred to the breakpoint. While the Anonymous class can perform a "surgical" debug for a specific object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.