19. Local inner class and anonymous inner class

Source: Internet
Author: User

 PackageCom.lei.duixiang;/*** Local Inner class * 1, if you need to use a local variable in a method, the local variable needs to be set to final type * 2, in other words, the inner class defined in the method can only access local variables of the final type in the method * This is because the local variable defined in the method is equivalent to a constant, its raw Life cycle Beyond Method run * Cannot change the value of a local variable in an inner class because the local variable is set to final@authorAdministrator * * Anonymous inner class OUTCLASS4*/InterfaceOutinterface {}//define an interface Public classOUTCLASS3 {//local inner class     PublicOutinterface doit (FinalString x) {        //define an inner class in the doit () method        classInnerClass2ImplementsOutinterface { PublicInnerClass2 (String s) {s=x;            System.out.println (s); }        }        return NewInnerClass2 ("doit"); }    /*** in the doit () method, first returns a Outinterface reference, and then inserts a custom inner class code in the return statement * because this class does not have a name, this is where the inner class becomes an anonymous inner class * essentially this     The function of an inner class is to create an object that implements an anonymous class on the Outinterface interface. * All implementations of the anonymous class are required to be written between braces, with the following syntax: * return new A () {...//inner class}; * After the anonymous inner class definition is complete, you need to add a mark * This semicolon is not an identity that represents the end of the definition inner class, but rather represents the identity that creates the Outinterface reference expression *@authorAdministrator **/     Public classOUTCLASS4 {//Anonymous Inner class         PublicOutinterface doit () {//defining the Doit () method            return NewOutinterface () {//declaring an anonymous inner class                Private inti = 0;  Public intGetValue () {returni;        }            }; }    }             Public Static voidMain (string[] args) {OUTCLASS3 out=NewOUTCLASS3 (); Out.doit ("Outinterface doit"); }}

19. Local inner class and 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.