Java Internal class summary
In short, the inner class defines a class within the class that we are familiar with.
Why do I need an internal class?
When we describe things, there are things in them, and we use inner classes to describe things.
Because internal things are using the contents of external things
Let me give you an example. The human body has a heart, blood, liver, spleen, lung-...... So how does the heart define?
We should define the heart as the inner class, because it uses the outer class (human body) of the blood, oxygen ...
Example 1: The basic structure of an inner class
classOut/*External Class*/{ Private intX=3; classInch/*Inner class*/ { Private inty Public voidShow () {System.out.println (x); } }}classdemo{ Public Static voidMain (String args[]) {out.in in=NewOut ().NewIn ();/*to create the format of an inner class object*/in.show (); }}
Run Result:3
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?
Because inner classes are free to use member variables of external classes (including private ones) without generating objects of external classes, this is the only advantage of inner classes
Like the heart can directly access the body's blood, rather than through the doctor to draw the blood
After compiling the program, two. class files are generated, 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
For other examples, see: http://www.cnblogs.com/nerxious/archive/2013/01/24/2875649.html
Definitions and access rules for internal classes in Java