Java Internal classes

Source: Internet
Author: User

A class defined inside a class is called an inner class, and the host of the inner class is the outer class. The Java inner class includes the member inner class, the local inner class, and the anonymous inner class.

1. member Inner class

Access modifier: The upper-level program unit of an external class is a package, so the outer class has only public and default permissions, and the upper-level program unit of the inner class is an external class, so the inner class has four access modifiers like the normal member, public,protected, the default , Private.

Class FileName: When a class file containing an inner class is compiled, two class files appear under the corresponding directory, one is Outer.class and the other is Outer$inner.class, where outer is the class name of the outer class and Inner the class name of the inner class.

Variable with the same name: an external class can sometimes be used, such as a member variable of an outer class, a member variable of an inner class, a local variable of an inner class, and the same name. this. Variable name, variable name, which distinguishes three variables.

Access problem: The outer class is always transparent to the inside, while the inner class is opaque, which means that the inner class can access the external member directly through the member name (which is also conditionally limited, as mentioned below), while the outer class cannot access the internal members directly through the member name. In summary, the way an inner class calls an external member is special, and the external class calls the members of the inner class in a traditional way.

1.1 Static Inner class

The static inner class is decorated by static members inside the class.

Accessing the inner class of another class in another class: The format is outer.inner name=new outer.inner (), it is obvious that this kind of name can well express the hierarchical logic, avoids the class name of a class to get too long. If Outer.Inner is considered as a whole, the static inner class is the same as the ordinary class declaration, and in fact it is basically the same, one of the differences is that there are four kinds of access rights for the static inner class, and there are only two external classes, and the other is that the static inner class accesses the external class and an external class accesses another An external class is slightly different, but in addition, the use of static internal classes and ordinary external classes is basically no two, and the C + + nested classes (c + + does not correspond to non-static inner classes in Java) are the same.

Mutual access issues for static internal classes and external classes:

When a method of a static inner class accesses a member of an external class, the non-static members of the outer class cannot be accessed because, as mentioned above, we can access the inner class of another class in another class, and because we can instantiate its inner class without instantiating it, it cannot be internally A non-static member of the outer class is called in the method (or initialization block) of the class, but the static inner class has no direct access to the static member of the outer class, and we can access the static member directly through the static member variable name (or method name) instead of invoking the class with the traditional way "class name. Member name" Member, which is also the second difference between the static inner class and the normal class described above.

When an external class accesses an inner class member, because the inner class is opaque, you must create an instance of the inner class and then call its members through the instance, and if you call a class member of a static inner class, you must pass the traditional format of "Inner. Member name".

Interface problem: Inside the interface can also define inner class, but must be static inner class, that is, can only use public static decoration, since only this one case , so public static can be omitted, and the public modifier of the method inside the interface, the property of the The public final modifier can be omitted as a reason. Interfaces can also be defined within an interface, but typically do not.

1.2 Non-static inner class

The non-static inner class is a major feature of the Java language, unlike the static inner class, which itself contains a reference to an instance of the external class, which can access any member of the outer classes, that is, if there is an instance of a non-static inner class, there must be an instance of its outer class. Note that non-static inner classes do not have static members, including static properties, static methods, and static initialization blocks.

The inner class of another class is accessed in another class: The format is outer.inner name=outer.new Inner (), where the lowercase Outer represents an instance of the outer class, which indicates that the instantiation of the non-static inner class must precede the actual Example of a partial class.

Mutual access to inner and outer classes: non-static internal classes Access external class members, including static and non-static members, without any restrictions, and can be accessed directly using the names of external members, but when an external class accesses an inner class member , static members cannot access any member of a non-static inner class, even if it is illegal to instantiate a non -static inner class, and the non-static inner class does not invoke an external class member, because the static member is not accessible to the non-static member's Principle . Quite simply, non-static inner classes are not static members . Non-static members of an external class access non-static inner class members by using the "inner class instance. member name" method.


2. Local inner class

The class defined within the method is a member inner class, and because the member inner class is accessible only within the method, the local inner class and its members do not need access control and static adornments. In the name of the class file, because the same class of different methods may be defined within the same name of the local inner class, the local inner class is the class file format "Outer$ninner.class, where n represents a number, meaning that the local inner class is the order of the method.


3. Anonymous inner class

Anonymous inner classes are suitable for classes that only need to be used once, often replacing function pointers with interfaces, such as the implementation of event listeners when writing desktop programs in Java. An anonymous inner class must inherit only one parent class or implement an interface. An anonymous inner class cannot be an abstract class and has no constructors, but can have initialization blocks. Local inner classes can access member variables of external classes, but they also follow the principle that static members cannot access non-static members, which must be decorated by final if the inner class needs to access the local variables of the method in which it resides.




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.