Anonymous class in Java

Source: Internet
Author: User
Tags anonymous
The anonymous class

The anonymous class can't have the name of the class, so can't refer to them. Must be created, as a new statement part of the statement.

This will use another form of new statement, as shown below:


New <a class or interface> <the class body>

That's the form of the new statement to declare a new anonymous class, it's a given class extended, or achieve a given interface. It also creates the Class A new instance of the, and as a statement of the results back. To expand the class and to achieve the interface is a new statement of the operands, followed by the Anonymous class of the subject.

If the Anonymous class to another class extensions, it is the main body of the can access the members, override the methods, and so on, this and other any standard class is the same. If the Anonymous class implements an interface, its main body must implement the interface method.

Note that the Anonymous class statement is at compile time, And instantiated at runtime. This means that the For Loop in a new statement will create the same Anonymous class a few examples, rather than create a few different anonymous an instance of a class.

Technically, the anonymous class can be regarded as a non-static inner class, so they have and Methods declared within the non-static inner class permissions and restrictions.

If you want to perform the tasks that need to be an object, but it's not worth it to create a new object (the reason may be that the class is too simple, or because it's only in a method for internal use, the anonymous class seems to be very useful. The anonymous class, especially suitable for the Swing applications quickly create an event handler.


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.