Small knowledge, big challenge! This article is part of the “Programmer’s Essentials
This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money
Inner class concept
Definition: To define a class inside another class. This class is called an inner class (nested class).
Classification: according to its location and form, it can be divided into member inner class, static inner class, local inner class and anonymous inner class.
1. Member inner classes
(Thinking of member variables and member methods, the inner class is at the same level as the member of the class.)
/ / outside class
class OuterClass01 {
// Member inner class
class InnerClass01 {}}Copy the code
OuterClass01$InnerClass01. Class, OuterClass01$InnerClass01. Class, OuterClass01$InnerClass01.
The bytecode name of the inner class of the member is: outer class name $Inner class name of the member. Class.
Creation method:
1.1 How to create member inner class objects
By calling the constructor of the member inner class.
Format: external class name. Inner class name Member Inner class object name = outer class object. new Inner class constructor ();
Method 1, OuterClass01. InnerClass01 InnerClass01 = (new OuterClass01 ()). The new InnerClass01 ();
Method 2, OuterClass01. InnerClass01 InnerClass01 = new OuterClass01 () new InnerClass01 ();
package cn.temptation;
public class Test {
public static void main(String[] args) {
// Create an outbound class object
OuterClass01 outerClass01 = new OuterClass01();
OuterClass01.InnerClass01 innerClass01 = outerClass01.new InnerClass01(a);
System.out.println(innerClass01);
// cn.temptation.OuterClass01$InnerClass01@15db9742}}Copy the code
Understand: OuterClass01 InnerClass01 InnerClass01 = OuterClass01. New InnerClass01 ();
Can be understood as: OuterClass01 InnerClass01 InnerClass01 = OuterClass01. (new InnerClass01 ()); That is, by the object name. Call a member of a class whose inner class is also a member of the outer class, so consider using the object name. Call this way and get the member inner class object, so get the member inner class object through the constructor of new member inner class
1.2 How to create member inner class objects
An object of a member inner class is created in an outer class by calling a member method of the outer class in which the member inner class resides
/ / outside class
class OuterClass02 {
// Member variables
private InnerClass02 innerClass02;
// Member methods
// Create multiple methods (inside a class, member methods can access members)
public InnerClass02 getInnerClass02(a) {
return new InnerClass02();
}
// Singleton creation method (lazy, inside the class, member methods can access members)
public InnerClass02 getInstance(a) {
if (innerClass02 == null) {
innerClass02 = new InnerClass02();
}
return innerClass02;
}
// Member inner class
class InnerClass02 {
// Set the constructor to private
private InnerClass02(a) {}}}package cn.temptation;
public class Test {
public static void main(String[] args) {
OuterClass02 outerClass02 = new OuterClass02();
// Create a member inner class object.
OuterClass02.InnerClass02 innerClass02 = outerClass02.getInnerClass02();
System.out.println(innerClass02);
//cn.temptation.OuterClass02$InnerClass02@15db9742
OuterClass02.InnerClass02 innerClass02Ex = outerClass02.getInnerClass02();
System.out.println(innerClass02Ex);
//cn.temptation.OuterClass02$InnerClass02@6d06d69c
System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
// Create a member inner class object (singleton)
OuterClass02.InnerClass02 innerClass02Demo = outerClass02.getInstance();
System.out.println(innerClass02Demo);
//cn.temptation.OuterClass02$InnerClass02@7852e922
OuterClass02.InnerClass02 innerClass02DemoEx = outerClass02.getInstance();
System.out.println(innerClass02DemoEx);
//cn.temptation.OuterClass02$InnerClass02@7852e922}}Copy the code
Features:
A member inner class can directly access non-static member variables and static member variables of its external class
/ / outside class
class OuterClass04 {
// A non-static member variable
public int i = 2;
private int j = 3;
// Static member variables
public static int x = 4;
private static int y = 5;
// Member inner class
class InnerClass04 {
// Member methods
public void showByInner(a) {
// A member inner class has access to non-static member variables of the external class in which it belongs (either public or private).
System.out.println(i);
System.out.println(j);
// Member inner classes have access to static member variables of the external class in which they belong (either public or private).System.out.println(x); System.out.println(y); }}Copy the code
External and inner class member variables are the same: variable values are retrieved using the nearest principle
/ / outside class
class OuterClass05 {
// A non-static member variable
public int i = 2;
// The member variable of the outer class has the same name as the member variable of the inner class
public int k = 4;
// Static member variables
public static int x = 6;
public static int z = 8;
// Member inner class
class InnerClass05 {
// A non-static member variable
public int j = 3;
// The member variable of the outer class has the same name as the member variable of the inner class
public int k = 5;
// Static member variables: a static member variable was defined in a non-static member inner class. Syntax error
The field y cannot be declared static in a non-static inner type, unless initialized with a constant expression
// public static int y = 7;
The field z cannot be declared static in a non-static inner type, unless initialized with a constant expression
// public static int z = 9;
// Member methods
public void showByInner(a) {
System.out.println(i); / / 2
System.out.println(j); / / 3
System.out.println(k); // 5 -- Inner class member variables (proximity rule)
System.out.println(this.k); // 5 -- Member variable of the inner class (this refers to the current member inner class object)
System.out.println(this); // cn.temptation.OuterClass05$InnerClass05@15db9742
// Question: What do I do if I want to use a member variable of the same name of the external class in the member method of the inner class?
// Create an external class object. Member variable fetch
System.out.println((new OuterClass05()).k); // 4 -- External class member variables
If a member's inner class object is created, its outer class object is created. If so, what is the access mode
// Answer: yes, the access form is the external class name. This
System.out.println(OuterClass05.this); // cn.temptation.OuterClass05@6d06d69c
System.out.println(OuterClass05.this.k); // 4 -- External class member variables
// Access static member variables of the external class, directly through the external class name. Static member variable
System.out.println(OuterClass05.x); / / 6
System.out.println(OuterClass05.z); / / 8}}Copy the code
Member methods of an inner class can access all member methods of the outer class (whether public or static).
/ / outside class
class OuterClass06 {
// Member methods
public void method1(a) {
System.out.println("Public non-static member methods of an external class");
}
private void method2(a) {
System.out.println("Private non-static member methods of an external class");
}
public static void method3(a) {
System.out.println("Public static member methods for external classes");
}
private static void method4(a) {
System.out.println("Private static member methods of external classes");
}
// Member inner class
class InnerClass06 {
// Member methods
public void showByInner(a) {
// Member methods of an inner class can access all member methods of the outer class (public or static)method1(); method2(); method3(); method4(); }}Copy the code
2, Static inner class (associate static member, use static modifier)
/ / outside class
class OuterClass08 {
// Static inner class
static class InnerClass08 {}}Copy the code
OuterClass08. Class OuterClass08 innerClass8.class OuterClass08 innerclass8.class OuterClass08 innerclass8.class OuterClass08 innerclass8.class Innerclass08.class the bytecode name of the static inner class is: InnerClass Name The name of the static inner class. Class.
Creation method:
Format: external class name. Inner Class Name Static inner class object name = new Outer class name. Static inner class constructor ();
package cn.temptation;
public class Test {
public static void main(String[] args) {
OuterClass08.InnerClass08 innerClass08 = new OuterClass08.InnerClass08();
System.out.println(innerClass08);
// cn.temptation.OuterClass08$InnerClass08@15db9742
}
}
Copy the code
Features:
Static inner class member methods, whether static or non-static, can only access the static member variables of the external class
/ / outside class
class OuterClass09 {
// A non-static member variable
public int i = 2;
private int j = 3;
// Static member variables
public static int x = 4;
private static int y = 5;
// Static inner class
static class InnerClass09 {
// Non-static member methods
public void showByInner(a) {
// A non-static member method in a static inner class cannot access a non-static member variable of the outer class in which it is located
// Cannot make a static reference to the non-static field i
// System.out.println(i);
// Cannot make a static reference to the non-static field j
// System.out.println(j);
// A non-static member method in a static inner class can access the static member variables of the external class
System.out.println(x); / / 4
System.out.println(y); / / 5
}
Static member methods
public static void showByInnerStatic(a) {
// A static member method in a static inner class cannot access a non-static member variable of the outer class it is in
// Cannot make a static reference to the non-static field i
// System.out.println(i);
// Cannot make a static reference to the non-static field j
// System.out.println(j);
System.out.println(x); / / 4
System.out.println(y); / / 5}}Copy the code
2, static inner class member methods, whether static or non-static member methods, can only access the static member method of the external class
/ / outside class
class OuterClass10 {
// Member methods
public void method1(a) {
System.out.println("Public non-static member methods of an external class");
}
private void method2(a) {
System.out.println("Private non-static member methods of an external class");
}
public static void method3(a) {
System.out.println("Public static member methods for external classes");
}
private static void method4(a) {
System.out.println("Private static member methods of external classes");
}
// Static inner class
static class InnerClass10 {
// Non-static member methods
public void showByInner(a) {
// Cannot make a static reference to the non-static method method1() from the type OuterClass10
// method1();
// Cannot make a static reference to the non-static method method2() from the type OuterClass10
// method2();
method3();
method4();
}
Static member methods
public static void showByInnerStatic(a) {
// Cannot make a static reference to the non-static method method1() from the type OuterClass10
// method1();
// Cannot make a static reference to the non-static method method2() from the type OuterClass10
// method2();method3(); method4(); }}Copy the code
3. Local inner class (thinking of local variables, located in the member methods of the external class)
/ / outside class
class OuterClass12 {
// Member methods
public Object showByOuter(a) {
// Local inner class
class InnerClass12 {}Instantiate the local inner class in the member method of the outer class
InnerClass12 innerClass12 = new InnerClass12();
returninnerClass12; }}Copy the code
Outerclass12. class outerclass121innerclass12. class Outerclass12. class Outerclass121innerclass12. class Outerclass12. class outerclass12. class outerclass12. class outerclass12. class outerclass12. class outerclass12. class outerclass12. class outerclass12. class outerclass12. class outerclass12. class outerclass12. class outerclass12. class outerclass12. Outer class name 1innerClass12.class The local inner class bytecode names are: Outer class name Number Local inner class name. Class. The number here is the corresponding number of the local inner class type, starting at 1 for the same kind of local inner class.
Note: It is not allowed to create more than one local inner class with the same name in a member method of the same outer class; Local inner classes with the same name can be defined in different member methods in an external class. These local inner classes can have the same name but different members.
Local members of the inner class methods to access in the local inner classes outside class members of the methods defined in the local variables, local variables can be used to final decorate, also can not use the final modification, but no matter whether to use the final modification, the local members of the inner class methods can only access (read), but not set (assignment)
Creation method:
// Local inner class objects created in member methods can only be returned through the member methods of the external class
OuterClass12 outerClass12 = new OuterClass12();
System.out.println(outerClass12.showByOuter());
The characteristics of
1, local inner class can directly access the external class’s non-static member variables and static member variables
/ / outside class
class OuterClass13 {
// A non-static member variable
public int i = 2;
private int j = 3;
// A member variable of the same name for the outer class and the local inner class
public int k = 4;
// Static member variables
public static int x = 6;
private static int y = 7;
public static int z = 8;
// Member methods
public void showByOuter(a) {
// Local inner class
class InnerClass13 {
// A non-static member variable
// A member variable of the same name for the outer class and the local inner class
public int k = 5;
// Static member variables
The field z cannot be declared static in a non-static inner type, unless initialized with a constant expression
// public static int z = 8;
// Member methods
public void showByInner(a) {
// A local inner class has access to the non-static member variables of the external class in which it belongs (public or private).
System.out.println(i); / / 2
System.out.println(j); / / 3
// A local inner class has access to static member variables of its own external class (public or private)
System.out.println(x); / / 6
System.out.println(y); / / 7
System.out.println(k); // 5 -- Non-static member variables of local inner classes (proximity rule)
System.out.println(this.k); // 5 -- Non-static member variable of the local inner class (this refers to the local inner class object)
System.out.println(this); // cn.temptation.OuterClass13$1InnerClass13@15db9742
System.out.println(OuterClass13.this); // cn.temptation.OuterClass13@6d06d69c
System.out.println(OuterClass13.this.k); // 4 -- A non-static member variable of the external class
System.out.println(OuterClass13.z); / / 8}}// Instantiate the local inner class object in the member method of the outer class
InnerClass13 innerClass13 = new InnerClass13();
innerClass13.showByInner();
}
2Non-static member methods and static member methods in a local inner class that have direct access to the outer class in which they are located/ / outside class
class OuterClass14 {
// Member methods
public void method1(a) {
System.out.println("Public non-static member methods of an external class");
}
private void method2(a) {
System.out.println("Private non-static member methods of an external class");
}
public static void method3(a) {
System.out.println("Public static member methods for external classes");
}
private static void method4(a) {
System.out.println("Private static member methods of external classes");
}
public void showByOuter(a) {
// Local inner class
class InnerClass14 {
// Member methods
public void showByInner(a) {
// A local inner class has access to the non-static member methods of the outer class in which it belongs (either using public or private).
method1();
method2();
// A local inner class has access to the static member methods of its external class (either public or private)method3(); method4(); }}// Instantiate the local inner class object in the member method of the outer class
InnerClass14 innerClass14 = new InnerClass14();
innerClass14.showByInner();
}
Copy the code
4. Anonymous inner classes (reminiscent of anonymous objects and arrays, inner classes have no name and need to have a class or interface)
// Define the interface
interface Foo {
public abstract void show(a);
}
// The implementation class of the interface
class FooImpl implements Foo {
@Override
public void show(a) {
System.out.println("Member methods of the implementation class of the interface"); }}/ / outside class
class OuterClass18 {
// Member methods
public Foo showByOuter(a) {
// Create a variable of the interface type, receive an object of the interface implementation type, and return it
// Foo foo = new FooImpl();
// return foo;
// A variant of the above notation, anonymous object notation
// return new FooImpl();
// An anonymous inner class that implements the Foo interface (at a location similar to a local inner class)
return new Foo() {
@Override
public void show(a) {
System.out.println("Anonymous inner class that implements the interface."); }}; }Copy the code
OuterClass18. Class. OuterClass18$1.class
The name of the anonymous inner class bytecode is: outer class name $no. class
Create a way
By calling a member method of the outer class in which the anonymous inner class resides
package cn.temptation;
public class Test {
public static void main(String[] args) {
OuterClass18 outerClass18 = new OuterClass18();
// Define a variable of the interface type to receive the return of an anonymous inner class object
Foo foo = outerClass18.showByOuter();
System.out.println(foo);
// cn.temptation.OuterClass18$1@15db9742
foo.show(); This statement actually calls the member methods of the interface implementation object (the anonymous inner class object)}}Copy the code
The characteristics of
Anonymous inner classes have no constructors, and users cannot create constructors, but the JDK actually generates constructors for anonymous inner classes
2. The member method of an anonymous inner class can access the member variable of its external class (whether it is public or private, whether it is static or non-static). The same variable is also the nearest principle
/ / interface
interface Sample {
public abstract void show(a);
}
/ / outside class
class OuterClass22 {
// Member variables
public int i = 2;
private int j = 3;
int k = 6;
public static int x = 4;
private static int y = 5;
// Member methods
public void showByOuter(a) {
// Local variables
int k = 7;
// Anonymous inner class
(new Sample() {
@Override
public void show(a) {
System.out.println(i); / / 2
System.out.println(j); / / 3
System.out.println(x); / / 4
System.out.println(y); / / 5
System.out.println(k); // 7 -- Local variables (proximity principle)
Error: k cannot be resolved or is not a field
// System.out.println(this.k);
System.out.println(this); // cn.temptation.OuterClass22$1@15db9742
System.out.println(OuterClass22.this); // cn.temptation.OuterClass22@6d06d69c
System.out.println(OuterClass22.this.k);/ / 6
}
}).show();
}
Copy the code