Java Internal class

Source: Internet
Author: User

The inner class is as its name implies: There are classes in the class, and the appearance of the Java inner class solves the variable or reference that is private modified in the class to be accessed directly.

member Inner class:

First look at the wording of the inner class.

public class Outer {private int x=9;class inner{//inner class Void Fun () {System.out.println ("Inner:" +x);}}}
Because the inner class and member variables are siblings, it is called the member inner class, and since it is a member inner class, the modifier for the member variable is fully applicable to the inner class. Let's look at the creation of an internal class instance

Outer.Inner Inner = new Outer (). New Inner ();
This gives an example of an inner class. Do you have such doubts, my God, what is this, here New Outer () represents the Outer class object, new Inner () represents the inner class object, and the preceding definition is to differentiate the inner class within that class, as you can imagine, two internal class names are the same, external class names are different, if written directly
Inner Inner = new Inner ();
Then the JVM is unsure of which inner class you're looking for, so it's understandable. So the question is, how to access the members of the external class in the inner class, by running the above code can see the console output is: Inner:9, here is actually omitted outer.this.x. Because an inner class holds an external class that holds a reference to an external class, you can directly access the members of the external class. OK, make a simple change to the above example:

Class Outer {private  int x=9;class inner{int x=6;void fun () {int X=3;<span style= "font-family:arial, Helvetica, SA Ns-serif; " >//①</span>system.out.println ("Inner:" +x);}}
This print result is inner:3, not ah, above just said omitted outer.this.x, the result should be 9 Ah, this relates to the nearest principle in Java, nearby has this variable not to go out to take. Then the above ① commented out, found that the printing result is 6, is still the nearest principle, how do not comment out ①, random access to the three x variables? Access outer x with Outer.this.x, Access inner x with this.x, it's ready. As mentioned above, the modifier of the member's inner class is fully applicable to the inner class, and so is static, so what is the characteristic of the inner class after the static modification? You can compare static member variables, access limitations, non-static access, only objects that do not require an external class when accessing an inner class, and some minor changes when creating an instance:

New Outer.Inner ();
This creates an instance of the inner class (Note: is an internal class instance, not an external class.), and then look at a section of code:

Class Outer {private  int x=9;class inner{static int x=0;//error void Fun () {System.out.println (This.tostring ())}}}
The error of the above notation, according to the JVM load class order, must first load the class to be able to load the static domain, loading non-static members. Static members are accessible without relying on an object, but the inner class is a member variable, not static, and an object of an outer class is required, so that the static of the inner class becomes meaningless, sowhen defining an intrinsic class static variable, the inner class itself must be static decorated。

Summary: The member inner class is the same level as the member variable, only the same level is called the member inner class, in order to apply to the modifier of the member variable. This is an attribute that other inner classes do not have, and the inner class can arbitrarily access the member variables of the outer class (including the private one), and the external class accesses the inner class member variable that requires an object in the outer class to establish the inner class to access. Inner classes are rarely public-decorated, except for interfaces.


local inner class:The local inner class is written locally as the name implies, here is an example:

Class Outer {public int x = 9;public void Fun () {class Inner {void Fun () {System.out.println (x);}}}
This is written in the method is the local inner class, the class is non-static, if not the new object, the fun () method of the class is not loaded and executed. The access permission modifier applies to member variables, so here inner local inner classes cannot use modifiers such as public private static. Here's a look at the code:

Class Outer {public int x = 9;public void Fun () {private int x = 6;//error, indicating that the final decoration is required before class Inner {void Fun () {System.ou T.PRINTLN (x);}}}
The above code is problematic, the error comment has been stated, the reason is that the local inner class in the method body, belongs to the stack memory, local local internal class in the heap memory, stack memory as the method executes immediately after the release of space, and heap memory is not, the need to wait until the JVM garbage collection disappears, then there is a problem, The method executes x disappears, but the object exists and accesses x, which is bound to report that the variable x exception is not found, in order to solve this problem, the final modification becomes a constant, change the storage space, and then solve the problem. Continue to modify the above code.

Class Outer {public int x = 9;public void Outerfun (final int ×) {//  x++;class Inner {void fun () {System.out.println (x) ;}} New Inner (). Fun ();}}
The main function accesses such

Outer.outerfun (1); Outer.outerfun (2);

Is this possible? The doubt here is that X is final and can be assigned two times. The answer is yes, they are not necessarily connected, when the fun method is pressed into the stack, the completion of the free space, and again pressed into the stack, so these two x is two different variables. The above x + + annotation method will correct the error.


Summary: An inner class can only access a final modified variable and cannot be decorated by a member modifier.

anonymous inner class:

As the name implies, is the name of the internal class, it is the shorthand format of the inner class, the definition of the inner class must inherit a class or implementation interface, the following is a non-anonymous inner class of the wording.

Abstract class absdemo{abstract void Show ();} Class Outer {public void fun () {class Inner extends absdemo{@Overridevoid Show () {System.out.println ("Inner:show");}}}

You can then use the inner object. The Shou () method can be called normally, just to call the Show method and write the code so complex, followed by the anonymous inner class, it is completely able to simplify the internal class code.

Abstract class absdemo{abstract void Show ();} Class Outer {public void fun () {new Absdemo () {@Overridevoid Show () {System.out.println ("Inner:show");}}. Show ();}}
This is the way anonymous inner classes are written, and we all know that abstract classes cannot create instances, unlike new Absdemo () with curly braces, which is equivalent to overriding the abstract method in Absdemo. The end of the curly brace is the subclass of the Absdemo (which can override the parent class method, which is necessarily a subclass), which is available when the Show method is called. In factThe essence of an anonymous inner class is an object of an anonymous subclass, the anonymous inner class is written in order to cover the method to simplify writing, although this simplifies the writing, but there are drawbacks, such as the parent class 5 methods, using anonymous inner class overrides will find that the readability is particularly poor, so the general situation to overwrite the parent class method of not more than 3, and then see an example:

Class Outer {public void fun () {new Absdemo () {@Overridevoid Show () {System.out.println ("Inner:show");} Subclass-specific methods void method () {System.out.println ("method");}}. Show ();}}
Call the parent class method at the back of the direct. Show () on the line, the subclass of the unique method how to call, do not think, can not be called, because the method subclass unique, subclass nameless son, there is no way to use the subclass of the instance to accept the object, if the use of the parent class is not defined by the method. This is also an anonymous inner class another drawback. Define an int x=5 on show (), which is also possible, Absdemo () {} Here is the curly brace of the class, which is the class that defines the member variable and the member method, which is really inappropriate, so that the anonymous inner class that appears in order to be simplified loses its meaning.


Summary: Anonymous inner classes are intended to be simplified and used to execute immediately after the class is created, and anonymous internal classes are often used in Android development.



Source: http://blog.csdn.net/u010829905/article/details/47667085

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Internal class

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.