Java reflection calls the inner class

Source: Internet
Author: User

1. Reflection calls the inner class of the default access permission

 PackageCom.blueStarWei.invoke;ImportJava.lang.reflect.Method;ImportCom.blueStarWei.invoke.OuterClass.InnerClass;classOuterclass { Public voidfunc () {System.out.println ("Outer Class."); }        classInnerclass {PrivateString Mname;  Publicinnerclass (String mname) { This. Mname =Mname; }        voidGetName () {System.out.println ("Name is:" +mname); }            }} Public classOuterinnerclass { Public Static voidMain (string[] args) {Try {            /*instantiating an inner class by reflection * The power is that when the inner class is privatized (private class innerclass), it can also call **/Class Outerclass= Class.forName ("Com.blueStarWei.invoke.OuterClass"); Class Innerclass= Class.forName ("Com.bluestarwei.invoke.outerclass$innerclass"); Method Method= Innerclass.getdeclaredmethod ("GetName"); //TODO Study it//the first parameter of an inner class newinstance must be a reference to an external class instanceMethod.invoke (Innerclass.getdeclaredconstructors () [0].newinstance (Outerclass.newinstance (), "Wei"));//Name Is:wei                        /*General Methods Instantiate inner classes * When the inner class is privatized, the inner class cannot be instantiated in this way **/Outerclass OuterClass1=NewOuterclass (); Innerclass InnerClass1= OuterClass1.NewInnerclass ("David"); Innerclass1.getname ();//Name Is:david}Catch(Exception e) {e.printstacktrace (); }    }}

2. Reflection call Static inner class

 PackageCom.blueStarWei.invoke;ImportJava.lang.reflect.Method;classOuterclass { Public voidfunc () {System.out.println ("Outer Class."); }        Static classInnerclass {PrivateString Mname;  Publicinnerclass (String mname) { This. Mname =Mname; }        voidGetName () {System.out.println ("Name is:" +mname); }    }} Public classStaticinnerclass { Public Static voidMain (string[] args) {Try {            /*instantiating an inner class by reflection*/Class Outerclass= Class.forName ("Com.blueStarWei.invoke.OuterClass"); Class Innerclass= Class.forName ("Com.bluestarwei.invoke.outerclass$innerclass"); Method Method= Innerclass.getdeclaredmethod ("GetName"); //static inner classes do not hold references to external classes, and do not need to pass references to external class instance objectsMethod.invoke (Innerclass.getdeclaredconstructors () [0].newinstance ("Wei"));//Name Is:wei                    } Catch(Exception e) {e.printstacktrace (); }    }}

3. Reflection calls Anonymous inner class

 PackageCom.blueStarWei.invoke;classOuterclass { PublicRunnable Runnable =NewRunnable () {@Override Public voidrun () {System.out.println ("This is innerclass."); }    };} Public classAnonymousinnerclass { Public Static voidMain (string[] args) {Try{Class Outerclass= Class.forName ("Com.blueStarWei.invoke.OuterClass"); //Treat an anonymous inner class as a member variable of an external classRunnable Runnable = (Runnable) Outerclass.getdeclaredfield ("Runnable"). Get (Outerclass.newinstance ());        Runnable.run (); } Catch(Exception e) {e.printstacktrace (); }    }}

For more information, please visit: http://www.cnblogs.com/BlueStarWei/

Java reflection calls the 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.