Java Foundation 04 Anonymous inner class

Source: Internet
Author: User

Inner class
(1) The class is defined inside another class, which is called an inner class.
Example: Class B is defined in Class A, and Class B is called an inner class.
(2) Access rules for internal classes
A: Members of external classes can be accessed directly, including private
B: External class to access internal class members, you must create an object
(3) Classification of internal classes
A: Member Inner class
B: Local inner class
(4) member inner class
A:private for the security of the data
B:static for ease of access

The member inner class is not static:
The external class name. Internal class Name Object name = new External class name. New internal class name ();
The member inner class is static:
The external class name. Internal class Name Object name = new External class name. Internal class name ();
(5) Questions of inner class of members (fill in the blanks)

class Outer {publicint num = ten; class Inner {publicint num =;  Public Viod Show () {int num =; SYSTEM.OUT.PRINTLN (num); System.out.println (this. Num); System.out.println (Outer.  This . num); }}}

Output: 30,20,10


(6) Local inner class
A: Local internal class access local variables must be final decorated.
B: Why?
Because local variables are used to disappear, the data for heap memory does not disappear immediately.
So, the heap memory is still using that variable, and the amount of change is gone.
In order for the value to exist, the final decoration is added.
With the anti-compilation tool, we see that, when final is added, the heap memory stores the value directly, not the variable name.
(7) Anonymous inner class (master)
A: A simplified form of a local inner class
B: Prerequisites
There is a class or interface
C: Format:
New class name or interface name () {
Override method;
}
D: Nature:
is actually a subclass of anonymous objects that inherit the class or implement an interface
(8) Use of anonymous internal classes in development
When we develop, we see abstract classes, or interfaces as parameters.
And this time, we know that what is actually needed is a subclass object.
If the method is called only once, we can use the format of the anonymous inner class to simplify.

As in multi-threading:

Anonymous inner class implementation of the thread class
public class Demo {public    static void Main (string[] args) {        thread t = new Thread () {public            void run () {
   
    for (int i = 1; I <= 5; i++) {                    System.out.print (i + "");}}}        ;        T.start ();    }}
   

operating Result:1 2 3 4 5

Anonymous inner class implementation of the Runnable interface
public class Demo {public    static void Main (string[] args) {        Runnable r = new Runnable () {public            void run () { for                (int i = 1; I <= 5; i++) {                    System.out.print (i + "");}}}        ;        Thread t = new Thread (r);        T.start ();    }}

operating Result:1 2 3 4 5

Java Foundation 04 Anonymous inner class

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.