Java--4 type of inner class

Source: Internet
Author: User

Inner class:

A non-static inner class

//non-static inner class//a non-static inner class can call local variables of an external class, whether or not private//to instantiate an inner class in an external class: Innerclass inner = new Innerclass ();//instantiate internal classes in other classes: 1. Test.innerclass inner = new test.new innerclass ();//2.Test t = new Test ();//T.innerclass inner = t.new innerclass ();//classtest{inti = 1; classinnerclass{ Public voidrun () {System.out.println ("outclass field =" +i); }            }       Public voidrun () {innerclass inner=NewInnerclass (); }    }classruntest{Test T=NewTest (); T.innerclass Inner= T.NewInnerclass (); //orTest.innerclass inner =NewTest.Newinnerclass ();} 

Two local inner classes

/*** Local Inner class * 1. Local inner classes and local variables function the same, only in local scope valid * 2. The member variable of the outer class can be called directly in the local inner class * 3. External classes Create local inner class objects Inclass Inclass = new Inclass (); * * *
    */ Public classOutclass {inti = 8;  Public voidrun () {classinclass{ Public voidrun () {System.out.println ("Run Field i =" +i); }} inclass Inclass=NewInclass ();    Inclass.run (); }    }

Three static inner classes

/*** Static Inner class * 1. Static internal classes also have access to external class member variables * 2. External class instantiation Inner class Inclass inclass = new Inclass (); * * **/ Public classOutclass {Static intt = 6; Static classinclass{inti = 5; intj =T; }     Public voidrun () {Inclass Inclass=NewInclass ();    System.out.println (INCLASS.I); }}

Four anonymous inner classes

Standard Creation Method

New anonymous{};

Use

/*** Anonymous Inner class * **/ Public classINNERCLASS4 { Public Static voidMain (string[] args) {Thread T=NewThread () { Public voidrun () { for(inti = 1; I < 6; i++) {System.out.println (i+ "Y");        }            }        };        T.start (); Runnable R=NewRunnable () { Public voidrun () { for(inti = 6; I < 11; i++) {System.out.println (i+ "U");        }            }        }; Thread T1=NewThread (R);    T1.start (); }}

Java--4 type of 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.