Today, let's review the inner classes in Java. There are generally several types of inner classes: static inner classes, member inner classes, anonymous inner classes, and method inner classes. The following will mainly introduce static inner classes and member inner classes, with the following main content:
- Overview
- Static Inner Classes
- Member Inner Classes
- Anonymous Inner Classes and Method Inner Classes
- Summary
Overview#
As the name suggests, an inner class is a class defined within another class. It is an independent class, and after compilation, it will generate a separate .class file. Its name will have the outer class name followed by the $ symbol. Inner classes can also be modified by private, default, protected, and public. The following is the syntax for inner classes, please refer to the specific example below:
/**
* Inner class
* @author jzman
*/
public class OutterClass {
// Static inner class
private static class StaticInnerClass{
}
// Member inner class
private class FieldInnerClass{
}
// Anonymous inner class
Runnable runnable = new Runnable() {
@Override
public void run() {
}
};
private void method() {
// Method inner class (local inner class)
class InnerClass{
}
}
}
Static Inner Classes#
- Static inner classes can have static members and non-static members.
- Static inner classes can directly access the static members and static methods of the outer class, but cannot access the non-static members and non-static methods of the outer class.
- In unrelated classes, you can directly create objects of static inner classes.
- Static inner classes have little connection with their outer classes, and their creation does not depend on the outer class.
/**
* Static inner class demo
* @author jzman
*/
public class StaticInnerClassDemo {
public static void main(String[] args) {
// Other classes can directly create instances of a non-private inner class
OutterClass1.InnerClass innerClassB = new OutterClass1.InnerClass();
InnerClass innerClassA = new InnerClass();
innerClassA.testStaticInnerClass();
//...
}
}
// Outer class
class OutterClass1{
// Non-static member variable
int ageA = 18;
// Non-static method
public void methodA() {
}
// Static member variable
static int ageB = 18;
// Static method
public static void methodB() {
}
private void methodC() {
// Outer class calls the method of the static inner class
InnerClass innerClass = new InnerClass();
innerClass.testStaticInnerClass();
}
// Static inner class
static class InnerClass{
public void testStaticInnerClass(){
// Static inner classes cannot access the non-static member variables and methods of the outer class
// System.out.println(ageA);
// methodA();
// Static inner classes can access the static members and methods of the outer class
System.out.println(ageB);
methodB();
}
}
}
Member Inner Classes#
- Member inner classes can access all members of the outer class.
- The creation of a member inner class object must depend on an outer class object. The inner class can only exist after the outer class exists.
- A member inner class is just a member variable of the outer class.
- Member inner classes cannot have static members, but constants are allowed.
/**
* Member inner class demo
*/
public class FieldInnerClassDemo {
public static void main(String[] args) {
// Member inner classes depend on their outer classes and cannot directly create instances of inner classes bypassing the outer class
// InnerClass innerClass = new InnerClass();
// Correct way to create an instance of an inner class in another class
OutterClass2 outterClass2 = new OutterClass2();
OutterClass2.InnerClass innerClass = outterClass2.new InnerClass();
innerClass.testFieldInnerClassMethod();
}
}
// Outer class
class OutterClass2{
// Non-static member variable
int ageA = 18;
// Non-static method
public void methodA() {
}
// Static member variable
static int ageB = 18;
// Static method
public static void methodB() {
}
private void methodC() {
// Outer class calls the method of the member inner class
InnerClass innerClass = new InnerClass();
innerClass.testFieldInnerClassMethod();
}
// Member inner class
class InnerClass{
// Member inner classes cannot have static members
// static int a = 10;
// Constants are allowed in member inner classes
static final int b = 10;
public void testFieldInnerClassMethod(){
// Member inner classes can directly access all member variables and methods (static and non-static) of the outer class
System.out.println(ageA);
methodA();
System.out.println(ageB);
methodB();
}
}
}
Anonymous Inner Classes and Method Inner Classes#
Anonymous inner classes do not have a specific name. Their implementation is actually implemented in the class body, and they ultimately return an anonymous object of the relevant class. Since the final return is an anonymous instance, a semicolon must be added after the anonymous inner class. Method inner classes are local inner classes, which are generally defined in the local position of a class. They can access all variables of the current code block and the outer class.
Summary#
The mutual calling of member variables between classes is actually accessing the member variables of objects. For example, the reason why a member inner class can access the member variables or methods of an outer class is because the inner class holds a reference to the outer class. Generally, the outer class can obtain the reference to the inner class through OutterClass.this. If the outer class wants to access the member variables or methods of the inner class, it must also hold a reference to the inner class. Therefore, when the outer class accesses the member variables or methods of the inner class, it must first create an object of the inner class, and then use the object to access the corresponding member variables or methods.