Anonymous inner classes in Java

Source: Internet
Author: User
The so-called anonymous inner class is an inner class that has no name and is usually used only once for this type of inner class.

An anonymous inner class must be declared as part of the new statement when it is created, and this will take another special form of the new statement.

Grammar:

This form of the new statement declares a novel anonymous class that expands on a given class, or implements a given interface, and also creates a new instance of that class and returns it as the result of the statement.

For example, define an interface as follows:

To define an implementation class for an interface:

The test procedure is as follows:

The result of the operation is:

Where the service is an interface, in mode two, an implementation class of the service interface is created using the anonymous inner class, and an object of the implementation class is returned.

Anonymous inner classes in Java

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.