Java Internal classes

Source: Internet
Author: User

1. Access rules for internal classes:

(1) An inner class can access members of an external class, including private, so that members of the outer 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 cannot access the inner class directly, and to instantiate an external class object to access the contents of the inner classes

2. 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 another class outside the

You can create an inner class object directly

Format: outer class name. Internal class Name Variable name = External Class object. Inner Class object

For example: Outer.Inner obj = new Outer (). New Inner ();

(2). When the inner class is on a member position, it can be decorated with the member modifier.

For example: Private, encapsulating inner classes in the outer class

Static: The inner class has static properties

When the inner class is modified by static, only static members in the outer class can be accessed directly, and access restrictions occur.

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 be static

classouter{Private intx = 3; classinner{intx = 4; voidfunction () {intX =5; System.out.println ("Inner:" +outer. This. x);//3System.out.println ("Inner:" + This. x);//4System.out.println ("inner:" +x);//5}  }}

Anonymous inner class:

1. Define the premise that the inner class must inherit a class or implement an interface

Anonymous inner class format: New parent class or interface {Defines the contents of a subclass}

In fact, the anonymous inner class is an anonymous subclass object, and the object is a bit fatter. Can be understood as an object with content

New Absdemo () {//absdemo is an abstract parent class

void Show () {System.out.println ("");} Overriding methods of the parent class

}. Show ();//The colored part is an object of an anonymous inner class

Anonymous inner class should not be more than 3, otherwise the readability is poor

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.