Today’s sharing started, please give us more advice ~

Nested class classification

  • Static inner classes (static nested classes/static member classes/static classes)

  • Inner class (non-statically nested class) Inner member class

Local inner class Anonymous inner class

  • Nested interface

Static inner class

Important conclusion: If a class is declared static(that is, the static modifier class), there is only one case where the class is a static inner class.

1. What classes, variables, and methods can be declared in static inner classes?

There are no restrictions, you can declare various types of classes, variables, methods, and static code blocks, subdivided into:

Class:

  • Enumeration class

  • Static inner class

  • The inner class

  • interface

Variables:

  • A static variable

  • The instance variables

Methods:

  • A static method

  • Instance methods

  • Static code block

2. What variables and methods can static inner classes access from outer classes?

Static inner classes can access any member of the enclosing class, including members of the enclosing class that are declared private, divided into:

  • Static variables and methods of peripheral classes (private included) : direct access

  • Instance variables and methods of the enclosing class (including private) : accessed through the instance object of the enclosing class

  • A static inner class is a class-like static variable that does not depend on the instance object of the enclosing class. It is considered a top-level class and can be accessed directly from the enclosing class.

3. Inheritance

In terms of inheritance, static inner classes are no different from peripheral classes, as long as access permits: any class can inherit from a static inner class, and a static inner class can inherit from any class (the class is not declared final) or implement any interface.

The inner class

1. The fine classification

  • Inner member class

  • Local inner Class (local class/local class)

  • Anonymous inner Classes (anonymous classes)

2. What classes, variables, and methods can be declared in inner classes?

Inner classes can declare instance variables, instance methods, and static variables of final type. Inner classes may not declare static members: this includes static variables, static methods, static inner classes, nested interfaces, and static initializer blocks. Subdivided into:

Class:

  • Only inner classes can be declared

  • You cannot declare enumerated classes, static inner classes, or interfaces

Variables:

  • Only instance variables can be declared, static variables of final type

  • Static variables cannot be declared

Methods:

  • Only instance methods can be declared

  • Static methods cannot be declared

  • Static code blocks cannot be declared

3. What variables and methods can the inner class access from the outer class?

There is no limit. All variables and methods (including private ones) of the enclosing class are directly accessible.

4. How does an inner class bind to a peripheral object?

Summary: When an inner class object is created (by calling the constructor of the inner class), the compiler implicitly declares a final member variable of the enclosing class type in the inner class, and then passes the object of the enclosing class through the constructor of the inner class to the final member variable to bind the inner class object to the enclosing class object.

Create an inner class object as follows:

When creating an inner class object, the system automatically passes the outer class object (outer) as a parameter to the inner class constructor, as follows:

Outer.Inner inner = outer.new Inner(outer);

5. Inheritance

An inner class can inherit from, or be inherited from, any class, as access permits.

Q: What’s the difference between an inner class and a static inner class in terms of inheritance?

A: An inner class’s object always depends on an outer object, so if A class A inherits an inner class, it must also bind to an inner class’s enclosing object, otherwise it will generate A compilation error.

1. Counterexample: A compilation error occurs

2. Cause of error:

If you want to create an object of class A, that is, A A = new A(); The constructor of class A is called, and the constructor of class A, which inherits Outer.Inner, is called.

When there is no valid peripheral class object, the binding between the inner class object and the peripheral class object cannot be realized, resulting in a compilation error.

3. The revised

Method 1: Pass A reference to the enclosing class in the class A constructor, call the inner class constructor through the enclosing class object,

It is equivalent to passing the object of the outer class to the constructor of the inner class, realizing the binding between the inner class object and the outer class object.

Method two: The outer class inherits the outer class, and the inner class inherits the inner class

Create the InnerA object of class A:

When creating the inner class A.innera object, we need to bind the outer object. The a reference is the object of the outer class.

Inner class A.innera inherits another Inner class, Outer.Inner. When a.innera calls the parent constructor, it also needs to pass the Outer object of the parent class.

Class A descends from Outer, because objects of subclasses can be used as objects of their parent class, so A reference is also A peripheral object of another Inner class, Outer.Inner.

6. Local inner classes

Local inner classes: classes declared in methods, constructors, initializer blocks.

A local inner class is not a member of a class and is structurally similar to a local variable, so you cannot use access modifiers (public, protected, private) or static modifiers.

Q: What is the difference between a local inner class declaration in an instance environment (instance methods, constructors, instance initializers) and a static environment?

A: Instance environment: The local inner class needs to be bound to the enclosing class, which implicitly generates a final reference in the class.

Static environment: Local inner classes do not need to be bound to peripheral classes.

Nested interface

Nested interface: an interface declared in a class or interface.

  • Nested interfaces are always static, whether declared in a class or an interface.

  • When a class implements an interface, it does not need to implement methods for nested interfaces.

Today’s share has ended, please forgive and give advice!