Some views of the Java inner class

Source: Internet
Author: User

Java internal class, I look at "Thinking in Java" When always feel ambiguous, struggled for several days, feel a part of the problem to think clearly, write an essay record, for later modification and review

What is an internal class?

The inner class is a class of classes, in a class to write a class, the inner class is divided into two, one is the ordinary inner class (Non-static nested Class), the second is the static inner class (Static nested Class), also known as nested class.

The ordinary inner class also includes two special types, one local inner class and the other anonymous inner class

What is the function of the inner class?

    1. The simplest feature: an effective code-behind mechanism that puts some functionality together to form a new class, and that the new class can arbitrarily access all members of the class. Effectively improve the encapsulation of the code, so that the written code is more easily understood
    2. Class-Logical groupings are implemented: for example, a Class A is only useful for Class B, A is a helper class for B, or just wants A to work on B, so logically a already belongs to Class B, and the inner class happens to implement this logic
    3. Together with interface, there is a true multi-inheritance: Because a class allows for multiple internal classes, and an inner class can freely access all members of a class, it is possible to implement different interfaces to accomplish multiple inheritance tasks in a true sense by inheriting different classes in different inner classes.
    4. The ability to provide closures through an internal class, or the ability to simulate closures (this part is not fully understood yet)

What is a closure package?

"Thinking in Java" is a very good quote:

A closure (closure) is a callable object that records information from the scope in which it was created.

With callbacks, an object can carry some information that allows it to invoke the initial object at some point in the future.

An inner class is an object-oriented closure, creating an inner class that gets a reference to an external class, through which we can invoke all members of an external class object, of course, which is called "Creating information about his domain." In addition to this, it is more secure to use the inner class to complete the closure, because a reference is returned, not a pointer, and the pointer and reference can manipulate the permissions more than a little.

Why can't a normal inner class contain static methods, static fields, and nested classes?

Because static modifies properties, methods are related to classes, not class objects. The access of the static member can be done directly through Classname.staticfield, and the creation of the inner class is associated with the outer class, that is, to use the inner class, you must create a related external class to provide the reference, and if the static member is allowed to exist, then you can pass the Outer.Inner . Staticfield access is not allowed to include static members, regardless of whether an external class exists.

However, a normal inner class can contain a member of the static final type, because the member of the static final type is a static constant, and at compile time, all the places that are used for that member are replaced with the corresponding child value constants. So a member of the static final type can only be initialized with a constant expression.

For example:

Class SS {public class Test1{public static final String str = "";p ublic static final Integer d = new Integer (25);//Error: the Field d cannot is declared static in a non-static inner type, unless initialized with a constant expression}}

Why

Why can't ordinary inner classes in Java have static data and static fields, and cannot contain nested classes: http://www.dewen.io/q/13793

Thinking in Java note, if there is a wrong place, also look at ^_^

Some views of the Java inner class

Related Article

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.