Java Inner Class--member inner class

Source: Internet
Author: User
Tags function definition

The inner class of the member means that the bread in an outer class has a non-static class, for example:

class outerclass{        // variables, function definitions ...         class  innerclass        {                         // variable, function definition ...         }}    

About the various uses of the members ' internal classes:

1. The member inner class cannot define static members for the following reasons:

For Java class loading order we know that first load the class, execute the static variable initialization, and then execute the object creation, if we want to execute the variable i initialization in the code, then we must first execute the load Outerclass, then load the Innerclass, and finally initialize the static variable i, The problem is that in loading innerclass, we can think of Innerclass as a non-static member of Outerclass, whose initialization must be done after the outer class object is created, and the innerclass must be loaded after the instantiation of Outerclass. The Java virtual machine requires that all static variables be completed before the object is created, thus creating a contradiction. In other words, the member inner class is required to be instantiated in the outer class before it can be loaded, in fact, the member inner class belongs to the class object, not the class, so if you have static members in Innerclass, then outerclass load, you must also load the Innerclass, There is no Outerclass instance object at this time, there will be no innerclass.

2. When the inner and outer classes have members with the same name (the variable or function, which refers to non-static):

If you want to get a variable in Innerclass, call it directly using the variable name or method name, or, if you want to call a member of Outerclass with the same name, pass "Outerclass.this. Member", which is the "outer class. This. Member".

3. When an external class has a member (variable or function, static, non-static can), but the inner class does not:

The members of the Innerclass call Outerclass are called directly by the variable name or function name.

4. When the external member is static (this does not take into account the case where the inner class is a static member, because this article only covers the case of the members inner class, and the member inner class does not allow the definition of static members):

When an external member is static, Innerclass is called directly through the variable name or function name.

5. External class, cannot directly manipulate members of inner class.

Can only be invoked through Innerclass objects, where innerclass is not considered static, (or does not take into account static methods in Innerclass, because member inner classes cannot have static members).

6. The private member of the external class, using rules in the inner class, is consistent with the above five.

7. If inheritance is involved, this class is also inherited

classtest{ Public Static voidMain (string[] args) {Human h=NewHuman (); Human.china HC=h.NewChina (); System.out.println (HC.CSTR);//will output the    }}classperson{ PublicString pstr= "Person"; classChina { PublicString cstr= "China"; }}classHumanextendsperson{//Other property methods}

Instance code:

classInnerclasstest { Public Static voidMain (string[] args) {Outerclass oc=NewOuterclass (); Outerclass.innerclass IC=oc.NewInnerclass ();//Create a new object for a member inner classOuterclass.innerclass ic=NewOuterclass ().NewInnerclass ();//this creates a new object for the member's inner class, except that it belongs to a newly created Outerclass objectSystem.out.println (IC. Getsamvar ());//calling a variable with the same name as the inner classSystem.out.println (); SYSTEM.OUT.PRINTLN (IC. Getnotsamevarfromouter ());//calling an external class without a variable with the same name as the inner classSystem.out.println (); SYSTEM.OUT.PRINTLN (IC. Getstaticvar ());//calling a static variable of an external classSystem.out.println (); SYSTEM.OUT.PRINTLN (IC. Callouterclassfunction ());//calling a non-static method of an external classSystem.out.println (); SYSTEM.OUT.PRINTLN (IC. Callouterclassstaticfunction ());//to invoke a static method of an external classSystem.out.println (); SYSTEM.OUT.PRINTLN (IC. Calloutersamefunctionnamewithinner ());//calling a non-static method in an external class with the same name as an inner classSystem.out.println (); SYSTEM.OUT.PRINTLN (IC. Callouterclassstaticfunction ());//calling a static method in an external classSystem.out.println (); }}//External Classclassouterclass{intOuterint=123; String outerstring= "I am outerclass ' s String"; String Samevar= "I am the Outerclass ' s Samevar";//Declare the same variable with the Innerclass.to show how to use the Outerclass and Innerclass var in the innerclass
        StaticString staticvar= "I am the Outerclass ' s Staticvar"; //static String staticsamevar= "I am the Outerclass ' s Staticsamevar";     PublicString outerclassfunction () {return"I am the Outerclass ' s function"; }     Public StaticString outerclassstaticfunction () {return"I am the Outerclass ' s Static function"; }//method with the same name as the inner class PublicString samefunction () {return"I am the Outerclass's function which have the same name with the Innerclass"; }
Inner classclassinnerclass {String Samevar= "I am the Outerclass ' s Samevar"; //static String staticsamevar= "I am the Outerclass ' s Staticvar"; //method with the same name as an external class PublicString samefunction () {return"I am the Innerclass's function which have the same name with the Outerclass"; } //variable with the same name as the outer class PublicString Getsamvar ()//This function was to distinguish the same name variable with the Outerclass { //return Samevar; //get the variables for the inner class returnOuterclass. This. Samevar;//get the Outerclass ' s Samevar } PublicString Getnotsamevarfromouter ()//Get the variable from Outerclass, and the variable are not in the Innerclass { returnouterstring; } PublicString Getstaticvar ()//To show how to use the Outerclass's static variable which not in the Innerclass { returnStaticvar; } PublicString callouterclassfunction ()//calling external class functions { returnOuterclass. This. Outerclassfunction (); } PublicString callouterclassstaticfunction () {returnOuterclass. This. Outerclassstaticfunction (); //return outerclass.outerclassstaticfunction ();//This both kinds of call, is OK } PublicString Calloutersamefunctionnamewithinner () {returnOuterclass. This. Samefunction (); } }}

The results are as follows:

All kinds of situations should be taken into account, if there are other circumstances, hurriedly message, I test, then continue to update this article. If there is an understanding of the wrong place, thank you for proposing ...

Java Inner Class--member 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.