Anonymous class explanation of Java

Source: Internet
Author: User

Reference 51542059

Anonymous classes, like names in Java, do not have a name identified in the class, of course, after compiling a name will be arranged.

Here is a simple example of an anonymous class:

 Public classClient { Public Static voidMain (string[] args) throws interruptedexception {Thread T=NewThread (NewRunnable () {@Override Public voidrun () {System. out. println ("hello,dusk!");          }          });      T.start (); }    }  

The Java language Specification describes the anonymous class as follows:

Declaration of an anonymous class:

The declaration of an anonymous class is automatically derived from a class instance creation expression by the Java compiler.

Anonymous classes can never be abstract.

An anonymous class is always implicitly final.

An anonymous class is always an inner class, and cannot be static.

Anonymous constructors:

An anonymous class cannot have a constructor that is explicitly declared. Instead, the Java compiler must automatically provide an anonymous constructor for this anonymous class. Anonymous Class C inherits from the parent class s then the anonymous constructor is in the following form:

    • If S is not an inner class or S is a local class in a static context, then the anonymous constructor has a formal parameter for each of the actual arguments in the class instance of C that are created in the expression.

The actual parameters of the instance creation expression of the class are used to determine a constructor for S CS, which invokes the same rule using the method.

The formal parameters of each anonymous constructor must be the same as the relevant parameters in CS.

This constructor explicitly includes super (...). Constructor call, which is the formal parameter of the constructor, in the order in which they are declared.

    • Otherwise, the first parameter of the constructor of C describes the value, enclosing the instance I with respect to S. The type of the parameter is the class type of S.

The constructor has an additional formal parameter for each class instance to create the actual arguments declared in the anonymous class. The nth formal parameter is related to the n-1 actual parameters.

The actual parameters of the instance creation expression of the class are used to determine a constructor for S CS, which invokes the same rule using the method.

The formal parameters of each anonymous constructor must be the same as the relevant parameters in CS.

This constructor explicitly includes super (...). Constructor call, which is the formal parameter of the constructor, in the order in which they are declared.

In all cases, the throws statement of the anonymous constructor must list all the check exceptions, including the exception thrown by the parent class constructor for explicit invocation of the statement and instance initialization of the anonymous class or the initialization of the variable.

Note: The signature of an anonymous constructor involves the possibility of an unreachable type (for example, the type that appears in the parent class constructor), which does not cause any compile-time and run-time errors.

We only need to pay attention to these points in actual use:

1. When using anonymous inner classes, we must inherit a class or implement an interface, but both cannot be combined, and can inherit only one class or implement an interface.
2. The constructor cannot be defined in an anonymous inner class.
3. No static member variables and static methods can exist in the anonymous inner class.
4. The anonymous inner class is a local inner class, so all the limitations of the local inner class also take effect on the anonymous inner class.
5. An anonymous inner class cannot be abstract, it must implement all the abstract methods of an inherited class or an implemented interface.

Compile-time Naming conventions:
    • The class file name for the inner classes is: Main class +$+ inner class name
    • The class file name for anonymous classes is: Main class +$+ (...)

Define a scene:

Package com.dusk.anonymous;  Public classAnonymousclasstest {PrivateRunnable r1=NewRunnable () {@Override Public voidrun () {System. out. println (1);      }      };  Public voidmethod1 () {Runnable R2=NewRunnable () {@Override Public voidrun () {System. out. println (2);      }          }; }       Public Static voidMain (string[] args) {Runnable R3=NewRunnable () {@Override Public voidrun () {System. out. println (3);      }              }; }  }  

From the code we can see that there are three anonymous classes defined:

We decompile it:

Package com.dusk.anonymous;    Import Java.io.PrintStream; classanonymousclasstest$1implements Runnable {anonymousclasstest$1(Anonymousclasstest paramanonymousclasstest) {} Public voidrun () {System. out. println (1);    }} package com.dusk.anonymous;    Import Java.io.PrintStream; classanonymousclasstest$2implements Runnable {anonymousclasstest$2(Anonymousclasstest paramanonymousclasstest) {} Public voidrun () {System. out. println (2);    }} package com.dusk.anonymous;    Import Java.io.PrintStream; classanonymousclasstest$3implements Runnable { Public voidrun () {System. out. println (3); }  }  
We can see that the numbers in the names of anonymous classes are exactly the same as where they appear in the code.

Anonymous class explanation of Java

Related Article

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.