Java Internal classes

Source: Internet
Author: User

Java Internal Classes

Java Inner class meaning: The class is also contained within another, the contained class is called an inner class.

' It's like a person is made up of physical outcomes such as the brain, limbs, organs, and the inner class is equivalent to one of the organs, such as the heart: it also has its own properties and behavior (blood, beating). Obviously, it is not possible to use properties or methods to represent a heart, because the heart also has its own properties and methods, so the inner class of the heart is needed in the body, as is the inner class (heart) in the outer class (the human body).

Commonality of inner classes:

(1), the inner class is still a separate class, after compiling the inner class is compiled into a separate. class file, but preceded by the class name and the $ symbol of the outer class. (2), internal classes can not be accessed in an ordinary way. An inner class is a member of an external class, so the inner class is free to access the member variables of the outer class, whether private or not. (3), the inner class is declared static, you cannot casually access the member variables of the outer class, at which point the inner class can only access static member variables of the outer class.

Knowledge point 1. The basic structure of the inner class
1 //External Class2 classouter{3     Private intAge = 10;4     //Inner class5     classinner{6         //internal methods in inner classes7          Public voidprint () {8 System.out.println (age);9         }Ten     } One } A  -  Public classTESTDEBUG10 { -      Public Static voidMain (string[] args) { theOuter.Inner Inner =NewOuter ().NewInner (); - inner.print (); -         //or access the inner class in a few ways. - /* + Outer Outer = new Outer (); - Outer.Inner Inner = outer.new Inner (); + inner.print (); A */     at     } -  -}

Operation Result:

10
View Code

At this point we ask, why use an internal class, directly define two separate classes is not OK?

Because inner classes are free to use the member variables of external classes (including private ones) without generating objects of external classes, this is the only advantage of inner classes, as the heart can directly access the blood of the body, rather than through the doctor's blood. and an inner class inherits from a class or implements an interface, the inner class's code action creates an object of its outer class. So you can assume that the inner class provides some kind of window into its outer class.

The most appealing reason for using inner classes is that each inner class inherits itself from the implementation of one (interface), so no matter whether the perimeter class has inherited an implementation of (an interface), there is no effect on the inner class. Some design and programming problems can be difficult to solve without the ability of an inner class to inherit multiple concrete or abstract classes. From this point of view, the inner class makes the solution of multiple inheritance complete. The interface solves some of the problems, while the inner class effectively implements "multiple inheritance."

After compiling the program, two. class files are generated, respectively Outer.class and Outer$inner.class

Outer.Inner Inner = new Outer (). New Inner (); This instantiation is the object used to generate the inner class, which has two small knowledge points to note

1. The outer at the beginning is to indicate that the inner class object to be generated is in the outer class of outer

2. An object of an outer class must be preceded to generate an object of the inner class, because the inner class is intended to access member variables in the outer class

Knowledge points 2. Variable access forms in internal classes
1 //External Class2 classouter{3     Private intAge = 10;4     //Inner class5     classinner{6         //member variables in an inner class7         Private intAge = 11;8         //internal methods in inner classes9          Public voidprint () {Ten             //Local Variables One             intAge = 12; ASYSTEM.OUT.PRINTLN ("local variable" +Age ); -System.out.println ("Inner class variable" + This. age); -SYSTEM.OUT.PRINTLN ("External class variable" +outer.) This. age); the         } -     } - } -  +  Public classTESTDEBUG10 { -      Public Static voidMain (string[] args) { +Outer.Inner Inner =NewOuter ().NewInner (); A inner.print (); at     } - -}

Operation Result:

Local variable 12 intrinsic class variable 11 external class variable 10
View Code

From the knowledge point 1, it can be found that the inner class has no member variables and local variables with the same name, the inner class accesses the member variables of the outer class directly without specifying the Out.this. Property name. Otherwise, local variables in the inner class will overwrite the member variables of the outer class. From the knowledge point 2 you can find that accessing the member variables of the inner class itself is available with the this. property name, which accesses the member variable of the external class requires the use of the Out.this. Property name.

Knowledge point 3: Static inner class
1 //External Class2 classouter{3     Private Static intAge = 10;4     Private intAge2 = 11;5     //Inner class6     Static classinner{7         //internal methods in inner classes8          Public voidprint () {9 System.out.println (age);Ten //System.out.println (Age2); //syntax error, cannot static reference to non-static field Age2 One         } A     } - } -  the  Public classTESTDEBUG10 { -      Public Static voidMain (string[] args) { -Outer.Inner Inner =NewOuter.Inner ();//the instantiation method at this time differs from the non-static inner class - inner.print (); +     } -  +}

Operation Result:

10
View Code

As you can see, if you statically internal static, then the inner class can only access static member variables of the outer class, with limitations second, because the inner class is static, so out.in can be seen as a whole, you can directly new out of the inner class object (through the class name to access static, It doesn't matter if you generate an external class object)

Knowledge point 4. Private Inner class
1 //External Class2 classouter{3     Private Static intAge = 10;4     //Inner class5     Private classinner{6         //internal methods in inner classes7          Public voidprint () {8 System.out.println (age);9         }Ten     } One      Public voidOuterprint () { A         NewInner (). print (); -     } - } the  -  Public classTESTDEBUG10 { -      Public Static voidMain (string[] args) { - /*//access to private internal classes when this method is wrong + Outer.Inner Inner = new Outer.Inner (); The instantiation method at this time differs from the non-static inner class - inner.print (); + */ AOuter Outer =NewOuter (); at outer.outerprint (); -     } -  -}

Operation Result:

10
View Code

If an inner class only wants to be manipulated by a method in an external class, you can declare the inner class with private, and in the code above, we have to create an object of the in class inside the out class to manipulate it.

Instead of using out.in in = new Out (). New in () generates an object of the inner class, that is, the inner class at this time is controlled only by the outer class. Like, my heart can only be controlled by my body,

Other people cannot access it directly.

Knowledge point 5. Method Inner Class
1 //External Class2 classouter{3     Private Static intAge = 10;4      Public voidPrint (Final intx) {5         classinner{6              Public voidInnerprint () {7 System.out.println (x);8 System.out.println (age);9             }Ten         } One         NewInner (). Innerprint (); A     } - } -  the  Public classTESTDEBUG10 { -      Public Static voidMain (string[] args) { -Outer Outer =NewOuter (); -Outer. Print (11); +     } -  +}

Operation Result:

1110
View Code

In the above code, we move the inner class into the method of the outer class, and then regenerate the inner class object in the method of the outer class to invoke the inner class method, if we need to pass in the parameter in the method of the outer classes at this time,

Then the method parameters of the outer class must use the final definition. As for final here there is no special meaning, just a form of representation.

Java Internal classes

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.