Summary of internal classes in Java

Source: Internet
Author: User

The inner class is not very well understood, but it is actually a class that contains another class.
As a person is composed of physical results, such as the brain, limbs, organs, or an internal class 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, but a class, and the heart is in the body, just as the inner class is inside the outside.


Example 1: The basic structure of an inner class

1 //External Class2 classOut {3     Private intAge = 12;4      5     //Inner class6     classIn {7          Public voidprint () {8 System.out.println (age);9         }Ten     } One } A   -  Public classDemo { -      Public Static voidMain (string[] args) { theOut.in in =NewOut ().NewIn (); - in.print (); -         //or access by sowing -         /* + out = new Out (); - out.in in = Out.new in (); + in.print (); A         */ at     } - } -Run Result: 12

From the above example, it is not difficult to see that the inner class actually seriously destroys the good code structure, but why use the inner class?
This is the only advantage of an inner class because the inner class is free to use the member variables of the outer class, including the private ones, without generating an object of the outer class.
As the heart can directly access the body's blood, instead of pumping blood through a doctor, the program compiles two. class files, respectively Out.class and Out$in.class
Where $ represents the out.in in the above program, out.in in = new Out (). The new in () can be used to generate an object of the inner class, which has two small knowledge points to note

1. The first out is to indicate which outer class the inner class object needs to be generated.
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.

Example 2: Variable Access form in an inner class

1 classOut {2     Private intAge = 12;3      4     classIn {5         Private intAge = 13;6          Public voidprint () {7             intAge = 14;8SYSTEM.OUT.PRINTLN ("local variable:" +Age );9System.out.println ("Inner class variable:" + This. age);TenSYSTEM.OUT.PRINTLN ("Outer class variable:" + out.) This. age); One         } A     } - } -   the  Public classDemo { -      Public Static voidMain (string[] args) { -Out.in in =NewOut ().NewIn (); - in.print (); +     } - } + Operation Result: ALocal variables: 14 atInternal class variable: 13 -External class variables: 12

As you can see from instance 1, inner classes have direct access to the member variables of the outer class without the same name as the member variable and the local variable. Instead of specifying the Out.this. Property name, the local variable in the inner class overrides the member variable of the outer class and accesses the member variable of the inner class itself. The property name, which accesses the member variable of the external class, requires the use of the Out.this. Property name.

Example 3: Static inner class

1 classOut {2     Private Static intAge = 12;3      4     Static classIn {5          Public voidprint () {6 System.out.println (age);7         }8     }9 }Ten   One  Public classDemo { A      Public Static voidMain (string[] args) { -Out.in in =Newout.in (); - in.print (); the     } - } -Run Result: 12

As you can see, if you statically internal static, the inner class can only access static member variables of the outer class, with limitations.

Example 4: Private Inner class

1 classOut {2     Private intAge = 12;3      4     Private classIn {5          Public voidprint () {6 System.out.println (age);7         }8     }9      Public voidOutprint () {Ten         NewIn (). print (); One     } A } -   -  Public classDemo { the      Public Static voidMain (string[] args) { -         //This method is not valid -         /* - out.in in = new Out (). New in (); + in.print (); -         */ +Out of out =Newout (); A out.outprint (); at     } - } -Run Result: 12

If an inner class only wants to be manipulated by a method in an external class, you can declare the inner class with private.
In the above code, we have to create an in class object inside the out class, and we can no longer use out.in in = new Out (). New in () creates an object of the inner class
That is, the inner class at this point can only be controlled by the outer class, as if, my heart can only be controlled by my body, others cannot access it directly.

Example 5: Method Inner class

1 classOut {2     Private intAge = 12;3  4      Public voidPrint (Final intx) {5         classIn {6              Public voidInprint () {7 System.out.println (x);8 System.out.println (age);9             }Ten         } One         NewIn (). Inprint (); A     } - } -   the  Public classDemo { -      Public Static voidMain (string[] args) { -Out of out =Newout (); -Out. Print (3); +     } - } + Operation Result:3 12

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 internal class method, and if we need to pass in the parameter in the method of the outer class, then the method parameters of the outer classes must use the final definition. As for final here there is no special meaning, just a form of representation.

Summary of internal classes in Java

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.