6.4 Internal Classes
-
Concept: an inner class is a class that is defined in another class.
-
Benefit: You can access all of the data for the class, including private Data.
Inner classes are hidden from other classes of the same package.
I. Internal class Access Objects
-
Inner and outer classes are relationships between classes, and not every external class has an instance domain of an inner class.
-
An object in an inner class always has an implicit reference to the outer class object that created it
-
Only inner classes can be private classes, whereas regular classes can only be package visibility or public visibility.
II. internal class rules
-
Can be used outer.new Inner (...) To create an inner class
-
All static domains declared by the inner class must be final, and the inner class cannot have a static method
-
The compiler translates the inner class into the class Name: Outer$inner's general class
III. Classification of internal classes
-
Local inner Class:
1) The local inner class cannot be declared with public or private, and its scope is limited to the block of the local class.
2) Advantages: The local method is completely hidden outside, not only access the external class, but also access to local variables, but those local variables must be final.
-
Anonymous inner Class:
1) Anonymous classes cannot have Parameters.
-
Static inner Class:
When: just to hide a class in another class, you do not need an object in which to reference the outer class.
A static inner class is exactly the same as all other inner classes except that it does not generate a reference to an external class.
Declaring inner classes automatically to static and public in an interface
This article is from the "red xiao" blog, please be sure to keep this source http://cnslp.blog.51cto.com/11387491/1918453
Java Internal class summary