The 15th chapter of the Java Development Handbook learning process internal class

Source: Internet
Author: User

Non-static inner class:

From outside the non-static inner class, it is perfectly possible to think of it as a non-static member of an outer class, no different from ordinary members. It's just that this member is no longer a basic data type, it is no longer an object reference, but rather a class that plays the role of a member by a class.

The access restriction modifier for an inner class can add private and protected in addition to the public and default of the external class.

Although the inner class is in the class body of the outer class, the compiled inner class and the external class each produce a class file.

Non-static inner classes can be used after an external class object has been created because the non-static inner class plays the role of the non-static member of the outer class, and not the static member exists only if the owning object exists.

An exchange of members between an inner class and an external class:

For an external class, the inner class is also one of the members, so when using the members of an external class in an inner class, it is equivalent to mutual access between ordinary members, as if they were called each other between different methods in a class. Therefore, any member of the outer class can be accessed in the inner class, even if it is private.

If you access non-static members of an inner class in an external class, you need to first create an object of the inner class. even private members of inner classes are accessible as external classes.

a non-static inner class cannot have a static member. in the Java loading process, the class is loaded first, and then the static stuff is initialized, and the Java compiler must ensure that all the static is allocated memory space before use, and then the non-static allocation of memory space only when used. So non-static innerclass have to be outclass to be instantiated before they exist, so static members of non-static innerclass are not initialized at the beginning (because the memory cannot be accessed in any way), which is contrary to the Java compiler principle.

Predefined object references this:

Predefined object reference This is a reference to the current object itself, but when an inner class object has a member variable of the same name as an outer class object, only this cannot be used to access the member variable of the same name from the inner class, since this refers to the inner class object itself.

If you need to use this for an external class object in an inner class, you can use the following syntax:

< external class names >.this.< member names that need to be accessed in an external class >

Local inner class:

When an inner class acts as a "local" role, its scope is the same as the local variable, only valid in the block of statements in which it resides.

A member of an external class can be accessed in the same way as a local inner class, but it is not possible to access ordinary local variables that are in a local part (and final can):

  • A normal local variable dies as the execution of the block of the statement ends, and the local inner class object created does not die with the end of the statement block. If you call a method that accesses a normal local variable in a local inner class object after the end of the statement block, there is a problem because the local variable that you want to access does not exist at this time.
  • The final local variable is stored in a different way from the normal local variable , and it will not disappear because of the end of the statement block, and it can persist for a long time.

If the local inner class is in a static method, you can access only the static members of the outer class, which is consistent with the rules for static method access members.

Static Inner class:

Because a static inner class is a static member of an external class, it cannot directly access non-static members of the outer class.

The difference between static and non-static inner classes:

A non-static inner class should have a shared relationship to a member with an object of an external class, which is part of an outer class object that is used to assist the outer class object to work.

The static inner class is actually out of control of the outer class, and the existence of an external class object is no longer required to create its object, essentially just a generic class that Wenmo in another class.

Anonymous inner class:

The anonymous inner class has no name, so the anonymous inner class also creates its object while declaring the class. The declaration of an anonymous inner class is either inherited or based on an implementation interface.

In the anonymous inner class class, you can override the parent class's methods, or provide your own new methods and members. Note, however, that because anonymous inner classes do not have a name, there is no way to declare a reference to an anonymous inner class type, so the new methods and members provided can only be used internally and cannot be used outside.

The use of anonymous inner class objects is generally done through polymorphism, with references to the direct or indirect parent class that it inherits from to the object. Therefore, only its overridden parent class method can be called by reference, and its own methods and members cannot be called.

In an anonymous inner class based on an interface implementation, because it has no name and cannot be inherited, the solid cannot be abstract, so all the methods in the interface must be implemented in the class body.

Non-static statement blocks are required to initialize the member variables of the parent class in an anonymous inner class.

The role of anonymous inner classes is reflected in:

In certain cases, anonymous inner classes can reduce redundant code. Because in real-world development, there are many classes that are written by themselves that need to be used only once, and do not need to create objects more than once, using an anonymous inner class to synchronize the class code with the created object.

Internal interface:

Interfaces that act as member roles are generally referred to as internal interfaces.

Define the internal interface in the class:

The internal interface defined in the class plays the role of an external member, and you need to be aware of the following points when using it:

  • The internal interface acts as a static member, regardless of whether the static decoration is used.
  • As with member inner classes, an internal interface acts as a member of an external class, requires all the rules of the member, and has all the capabilities of the member. For example, the internal interface can be decorated by the member access restriction modifier, and the normal interface does not.
  • When the internal interface is decorated with the private access restriction modifier, it means that the interface can only be implemented by an inner class in its outer class.
  • The internal interface cannot play a local role.

The 15th chapter of the Java Development Handbook learning process internal 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.