Learning Diary (12) Java nested classes and inner classes

Source: Internet
Author: User

Nested classes and Inner classes: classes defined inside a class are called nested classes, where nested classes that do not have static adornments are the inner classes we normally call, whereas nested classes that are modified by static are not commonly used. In some places there is no distinction between nested classes and inner classes, which are directly nested classes, which are called inner classes, without the notion of nested classes. And what I usually hear is basically a straightforward inner class, which may be more common.

The scope of an inner class is limited by the scope of the class in which it is loaded, and the inner class can access the members of the outer class, including private adornments, because it is treated as a member of an external class, and the members of a class can access each other, but in turn the external class cannot access the implementation details of the inner class.

Inner classes can be defined anywhere outside the class, and naturally also include methods of external classes, internal classes defined in methods of external classes are referred to as local inner classes, and vice versa to member inner classes, except that usually local internal classes are not used much.

A member inner class is a member of a class, such as a method, property, and constructor for a class, and a local inner class and an anonymous inner class are not members of a class.

Typically, when a class is used only once, anonymous internal classes may be used, which is relatively convenient.

The inner class of the static modifier is often called a static inner class, sometimes called a class inner class, which is class-related and not object-related, so it can access only the class members of the outer class, which is also members of the static adornment. It is important to note that even the instance method inside the static inner class cannot access the non-class members of the outer class.

A static inner class can contain static members or can contain non-static members.

External classes still do not have direct access to members of static inner classes, but you can access the class members of static inner classes through the class masterpieces of static inner classes, or you can access non-class members (instance members) through objects of static inner classes.

Java also allows internal classes to be defined in the interface, but the inner classes here can only be static inner classes, which are decorated by default public static.

Internal classes can also be used outside of the outside class.

An anonymous inner class cannot be defined as an abstract class, and an anonymous inner class cannot define a constructor.


Learning Diary (12) Java nested classes and inner 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.