Benefits of Java internal and anonymous classes

Source: Internet
Author: User
Tags stack trace
Anonymous classes (anonymous inner classes) cannot be referenced because they cannot have names. They must be declared as part of the new statement at creation.

This requires another new statement, as shown below:

New <class or interface> <class subject>

This new statement declares a new anonymous class, which extends a given class or implements a given interface. It also creates a new instance of that class and returns it as the result of the statement. The class to be extended and the interface to be implemented are the operands of the new statement, followed by the subject of the anonymous class.

If an anonymous class is extended to another class, its subject can be a member of the class, override its method, and so on, which is the same as any other standard class. If an anonymous class implements an interface, its subject must implement the interface method.

Note: anonymous classes are declared at compilation and instantiated at runtime. This means that a new statement in the For Loop will create several instances of the same Anonymous class, rather than creating several instances of different anonymous classes.

Technically, anonymous classes can be considered non-static internal classes, so they have the same permissions and restrictions as non-static internal classes declared in the method.

Suppose that the task needs an object, but it is not worth creating a new object (the reason may be that the required class is too simple, or because it is only used within a method ), anonymous classes are very useful. Anonymous classes are especially suitable for quickly creating event handlers in swing applications.

In Java, anonymous classes are used to process events (event handle ). But they are also very helpful for debugging. The following describes how to use anonymous classes to simplify your debug.

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 there is a button that is invisible. You want to know why this exception occurs, 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

Internal class

To put it simply, "internal class" is the class declared inside another class. From Java 1.1, you can declare another class in one class, which is very similar to declaring fields and methods. Classes that wrap internal class declarations are called "external classes ".

In fact, the Java language specification allows you to do more things, including:

Declare a class in another class or interface.
Declare an interface in another interface or class.
Declare a class in a method.
Class and interface declaration can be nested in any depth. Code for more optimized internal classes

The internal and anonymous classes are two outstanding tools provided by Java 1.1. They provide better encapsulation, and the result is that the Code is easier to understand and maintain, so that the relevant classes can exist in the same source code file (thanks to internal classes ), and avoid a program from generating a large number of very small classes (thanks to anonymous classes ).

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.