Java-Summary of mutual access between internal and external classes

Source: Internet
Author: User

An internal class is also called a nested class. It can be understood as a common member of an external class. The internal class can access the external class freely, and the rules are the same as static. (You must first create an object when accessing non-static internal classes.) specifically: non-static internal classes directly access [java] public class Outter {int I = 5; static String string = "Hello"; class Inner1 {void Test1 () {System. out. println (I); System. out. println (string) ;}}< span style = "font-family: Arial, Helvetica, the non-static method of the sans-serif "> </span> static internal class. Because the static method needs to create an instance before accessing non-static external members, you must create a new external class before accessing I. [Java] public class Outter {int I = 5; static String string = "Hello"; static class Inner2 {void Test1 () {System. out. println (new Outter (). i); System. out. println (string) ;}} static internal class static method rules as above [java] public class Outter {int I = 5; static String string = "Hello "; static class Inner2 {static void Test2 () {System. out. println (new Outter (). i); System. out. println (string) ;}}< span style = "font-famil Y: Arial, Helvetica, sans-serif "> </span> external class access internal class direction: because the internal class is understood as a common member of the external class, therefore, a new object is required for external access. Non-static access to non-static internal class members: new internal class object [java] public class Outter {void Test1 () {System. out. println (new Inner1 (). i);} class Inner1 {int I = 5; // static String string = "Hello"; definition error!} For static access to non-static internal class members, the static method Test2 requires a new external class to access non-static Inner1; for continued access to non-static member I requires a new internal class, so the access rule is: new Outter (). new Inner1 (). i. [Java] public class Outter {static void Test2 () {System. out. println (new Outter (). new Inner1 (). i);} class Inner1 {int I = 5; // static String string = "Hello"; definition error! }}< Span style = "font-family: Arial, Helvetica, sans-serif"> </span> members of a non-static method accessing a static internal class first refer to "external class. internal class "access to internal class. If you access a static member, you must first access the new member. If you access a non-static member, you can directly access it. [Java] public class Outter {void Test1 () {Outter. inner2 inner2 = new Outter. inner2 (); System. out. println (inner2. I); System. out. println (inner2.string); System. out. println (Outter. inner2.string);} static class Inner2 {int I = 5; static String string = "Hello" ;}< span style = "font-family: Arial, Helvetica, sans-serif "> </span> non-static methods access non-static internal class members first. internal class "access to internal class, and then new to access static members. [Java] public class Outter {void Test1 () {Outter. inner1 inner2 = new Outter. inner1 (); System. out. println (inner2. I);} class Inner1 {int I = 5; // static String string = "Hello"; definition error!} When an anonymous internal class accesses an external member variable, the final keyword should be added before the member variable. [Java] final int k = 6; (Button) findViewById (R. id. button2 )). setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub System. out. println (k );}});

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.