The inner classes in Java are divided into four:
- member Inner class
- Static Inner class
- Local inner class
- Anonymous inner class
1. member Inner class:
Defined inside another class (outer Class), and with member methods and properties called members Inner class, ... A non-static method equivalent to an outer class, which, if modified by static, becomes a static inner class.
- ) The static keyword cannot exist in a member inner class, that is, you cannot declare static properties, static methods, static blocks of code, and so on. " non-static inner classes can also define static members but need to have final keyword adornments at the same time, whereas static methods must still be defined in static internal classes or non-intrinsic classes, given that they cannot be decorated with a final.
- ) Creates an instance of a member's inner class using: the external class name. Internal class Name Instance name = External class instance name. The new inner class constructor method (parameter) is understood to implicitly hold a reference to the outer class object that created it.
- ) used when accessing member methods and properties of an external class in a member's inner class: the external class name. This. Member method/property.
- ) The inner class generates a separate class file after compilation, containing the definition of the class, so that the methods and variables defined in the inner class can be the same as the methods and variables of the parent class. For example, the class files for the three classes defined above are: Mytest.class, Outer.class, and outer$inner.class three files.
- ) External classes cannot directly access methods and properties of members ' inner classes and need to be accessed through an instance of an inner class.
- ) When inheriting an inner class from an outer class class, it is necessary to pass in the instance object of the parent class in its construction method. and the "external class instance name. Super (internal class parameter)" is called in the first sentence of the constructor method.
member Inner class ... Equivalent to a non-static method class outer{ private int a = 3; Private Inner in; Public Outer () {in = new Inner (); } public int Getinnera () { return in.a; A variable that references an inner class needs to pass the instance } public class Inner {public int a = 2; public void DoSomething () { //calls the properties of the external class System.out.println (MEMBERINNER.THIS.A);//This piece should be noted ... Very IMPORTANT!!! System.out.println (a);}} public class Test3 {public static void Main (string[] args) { Outer.Inner Inner = new Outer (). New Inner ();//Non-static Inner class to new instance inner.dosomething (); }} Class Extender extends outer.inner{public Extender (Outer Outer) { //external class instance name. Super (internal class parameter list) Outer.super (); } }
2. Static Inner class
A member inner class that uses static adornments is called a static inner class.
You can understand this: a class that is sibling to an external class, or a static member called an external class. The comparison with the members inner classes is as follows:
Description |
member Inner class |
Static Inner class |
Static members |
Static members need to have a final keyword modifier at the same time |
OK |
Static methods |
Not defined |
OK |
Accessing external classes non-static properties/methods |
External class name. This. Member method/property |
No |
External class access Inner class |
needs to be accessed through an instance of an inner class |
needs to be accessed through an instance of an inner class |
Create an instance |
The external class name. Internal class Name Instance name = External class instance name. New Inner class constructor method (parameter) |
The external class name. Internal class Name Instance name = new External class name. Internal class name (parameter) |
The compiled class file |
A separate class file (the methods and variables in the so inner class can have the same name as the parent class's methods and variables), the outer class $ inner class. Class |
A separate class file (the methods and variables in the so inner class can have the same name as the parent class's methods and variables), the outer class $ inner class. Class |
Other |
When you inherit an inner class from an outer class class, you need to pass in the instance object of the parent class in its construction method. and the "external class instance name. Super (internal class parameter)" is called in the first sentence of the constructor method. |
No |
/**
* member inner class and static inner class summary:
* 1. Static inner classes can have static members (methods, properties), and non-static inner classes cannot have static members (methods, properties).
* 2. Static inner classes can access only static members of external classes, not static inner classes, which access all members of the external class (Methods, properties).
* 3. Instantiate a non-static method of an inner class:
* Mr. A. As an instance of an external class object
* Outclasstest Oc1 = new Outclasstest ();
* B. Generating an inner class object from an object instance of an external class
* Outclasstest.innerclass No_static_inner = oc1.new innerclass ();
* 4. Instantiate a method of a static inner class:
* A. Direct instantiation of an inner class object without relying on an instance of an external class
* Outclasstest.innerstaticclass inner = new Outclasstest.innerstaticclass ();
* B. Call the method or static variable of the internal static class, calling directly through the class name
* OutClassTest.InnerStaticClass.static_value
* OutClassTest.InnerStaticClass.getMessage ()
*/
3. Local inner class
A class defined within a code block, a method body, a scope (a piece of code enclosed in curly braces "{}") is called a local inner class.
- Local inner classes can only be used in code blocks, within the method body, and in scopes (such as creating objects and using class objects, etc.)
- Local inner class accesses a local variable within the scope, which requires a final decoration.
- ) can be declared as an abstract class using the abstract adornment.
4. Anonymous inner class
Why anonymous internal classes: primarily for abstract classes and interfaces that cannot directly create objects
New Parent class construction Method (parameter) { //Note: The method name must already exist in the parent class modifier returns the parameter type method name (parameter list) { ... } }
- Can only be used once, and once the instance is created, the class definition disappears immediately (you want to use the knowledge of reflection for multiple uses)
- You must inherit a class (abstract, non-abstract) or implement an interface. If the parent class (or parent interface) is an abstract class, the anonymous inner class must implement all of its abstract methods.
- Cannot be an abstract class, because an anonymous inner class creates an instance immediately after it is defined.
- The constructor method cannot be defined, the anonymous inner class has no class name and cannot define a constructor method, but the anonymous inner class has all the same constructor methods as the parent class.
- You can define code blocks for instance initialization, but you cannot define static blocks of code.
- You can define new methods and properties (you cannot use the static adornment), but you cannot explicitly call it through the form "instance name. Method Name (parameter)", because using new creates a "Top transformed object" (that is, the parent class declaration points to the subclass object).
- ) is a local inner class, so conform to the requirements of the local inner class.
Description |
member Inner class |
Anonymous inner class |
Static members |
Static members need to have a final keyword modifier at the same time |
Not defined |
Static methods |
Not defined |
Not defined |
Accessing external classes non-static properties/methods |
External class name. This. Member method/property |
External class name. This. Member method/property |
External class access Inner class |
needs to be accessed through an instance of an inner class |
needs to be accessed through an instance of an inner class |
Create an instance |
The external class name. Internal class Name Instance name = External class instance name. New Inner class constructor method (parameter) |
As above: Parent class Instance name = New Parent class () {} |
The compiled class file |
A separate class file (the methods and variables in the so inner class can have the same name as the parent class's methods and variables), the outer class $ inner class. Class |
A separate class file, using the class $ number. class |
Other |
When you inherit an inner class from an outer class class, you need to pass in the instance object of the parent class in its construction method. and the "external class instance name. Super (internal class parameter)" is called in the first sentence of the constructor method. |
No |
Four internal classes in Java