Definition principles for Java internal classes

Source: Internet
Author: User

/*
Access rules for internal classes:
1, the inner class can access the members of the external class directly, including the private.
Members in an external class can be accessed directly because the inner class holds a reference to an external class, in the form of an external class name. This
2, the outer class must establish an inner class object to access the inner class.

Access format:
1, when the inner class is defined on the member position of the outer class, and is not private, it can be in an external other class.
You can create an inner class object directly.
Format
The outer class name. Internal class Name Variable name = External Class object. Inner class object;
Outer.Inner in = new Outer (). New Inner ();

2, when the inner class is in the member position, it can be decorated by the member modifier.
For example, private: Encapsulates inner classes in the outer class.
Static: The inner class has static properties.
When an inner class is modified by static, only static members in the outer class can be accessed directly. Access restrictions have occurred.

In other external classes, how do you directly access the non-static members of the static inner class?
New Outer.Inner (). function ();

In other external classes, how do you directly access static members of static internal classes?
Uter. Inner.function ();

Note: When a static member is defined in an inner class, the inner class must be static.
When a static method in an external class accesses an inner class, the inner class must also be static.




When describing things, there is something inside the thing, which is described by an inner class.
Because internal transactions are using the contents of external things.

Class Body
{
Private Class Xinzang
{

}

public void Show ()
{
New Xinzang ().
}

}





*/
Class Outer
{
private static int x = 3;


Static class inner//statically internal classes
{
static void function ()
{
System.out.println ("Innner:" +x);
}
}

Static Class Inner2
{
void Show ()
{
System.out.println ("Inner2 show");
}
}

public static void Method ()
{
Inner.function ();
New Inner2 (). Show ();
}

}


Class InnerClassDemo2
{
public static void Main (string[] args)
{
Outer.method ();
Outer.Inner.function ();
New Outer.Inner (). function ();
Directly accesses members in the inner class.
Outer.Inner in = new Outer (). New Inner ();
In.function ();
}
}

Definition principles for Java internal classes

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.