Java development job interview was asked what to do with nested classes

  • 2021-11-01 03:10:37
  • OfStack

Directory nested classes classify static inner classes 1. What classes, variables, and methods can be declared in static inner classes? 2. What variables and methods of peripheral classes can static inner classes access? 3. Inheriting internal classes 1. Classification 2. What classes, variables and methods can be declared in internal classes? 3. What variables and methods of the outer class can the inner class access? 4. How do inner classes bind peripheral objects? 5. Inheritance Aspects 6. Summary of Nested Interfaces for Local Internal Classes

Nested class classification

Static inner classes (static nested classes/static member classes/static classes) Inner class (non-static nested class)

Internal member class

Local inner class

Anonymous inner class

Nested interface

Static inner class

Important conclusion: If a class is declared as static (that is, static modifies class), there is only one case in which the class is a static inner class.

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

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

Class:

Enumeration class Static inner class Inner class Interface

Variables:

Static variable Instance variable

Methods:

Static method Instance method Static code block

2. What variables and methods of peripheral classes can static inner classes access?

Static inner classes can access any member of the peripheral class, including those declared as private in the peripheral class, as follows:

Static variables and methods of peripheral classes (including private ones): direct access Peripheral class instance variables and methods (including private): accessed through the instance object of the perimeter class Static inner classes are similar to static variables of classes, which do not need to depend on instance objects of peripheral classes. They can be regarded as top-level classes and can be accessed directly through peripheral classes.

3. Inheritance aspects

Static inner classes are no different from peripheral classes in terms of inheritance, as long as access permissions allow: Any class can inherit a static inner class, and a static inner class can inherit any class (which is not declared as final) or implement any interface.


public class OuterClass {
    //  Static variable 
    final static boolean FLAG_VALUE = true;
    private static String name = "Outer Class";
    //  Instance variable 
    private int age;
    //  Static method 
    private static String getName() {
        return name;
    }
    //  Instance method 
    private void setAge(int age) {
        this.age = age;
    }
    //  Static inner class 
    public static class StaticInner {
        //  Declare static variables 
        final static int x = 1;
        static int y = 2;
        //  Declare instance variables 
        int a;
        //  Declare static code blocks 
        static { }
        //  Declare an enumeration class 
        enum InnerEnum { }
        //  Declare static inner classes 
        static class Inner2 { }
        //  Declare interface 
        interface Inner3 { }
        //  Declare inner classes 
        class Inner4 { }
        //  Declare static methods 
        static void OperateStatic() {            
            System.out.println(" Static inner classes directly access static variables of peripheral classes: name = " + name + ", FLAG_VALUE = " + FLAG_VALUE);
            System.out.println(" Static inner classes directly access static methods of peripheral classes: getName : " + getName());
            //  Static inner classes cannot directly access instance variables and methods of peripheral classes 
            //age = 30;
            //setAge(30);
            //  Access instance variables and methods of peripheral class objects by declaring peripheral class objects 
            OuterClass outerClass = new OuterClass();
            outerClass.age = 30;
            outerClass.setAge(30);
        }// Join Java Develop and exchange samples: 7565848221 Blowing water and chatting 
        //  Declare an instance method 
        void operate() {
            System.out.println(" Static inner classes directly access static variables of peripheral classes: name = " + name + ", FLAG_VALUE = " + FLAG_VALUE);
            System.out.println(" Static inner classes directly access static methods of peripheral classes: getName : " + getName());
            //  Static inner classes cannot directly access instance variables and methods of peripheral classes 
            //age = 30;
            //setAge(30);
            //  Access instance variables and methods of peripheral class objects by declaring peripheral class objects 
            OuterClass outerClass = new OuterClass();
            outerClass.age = 30;
            outerClass.setAge(30);
        }
    }
}

Inner class

1. Breakdown

Internal member class

Local inner class (local class/local class)

Anonymous inner class (anonymous class)

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

Internal classes can declare instance variables, instance methods, and static variables of type final.
Inner classes cannot declare static members: including static variables, static methods, static inner classes, nested interfaces, static initialization blocks.
Subdivided into:

Class:

Only inner classes can be declared Cannot declare enumerated classes, static inner classes, interfaces

Variables:

Only instance variables, static variables of final type can be declared 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 of the outer class can the inner class access?

Without restriction, all variables and methods (including private ones) of peripheral classes can be accessed directly.


public class OuterClass {
    //  Static variable 
    final static boolean FLAG_VALUE = true;
    private static String name = "Outer Class";
    //  Instance variable 
    private int age;
    //  Static method 
    private static String getName() {
        return name;
    }
    //  Instance method 
    private int getAge() {
        return age;
    }
    //  Inner class 
    public class Inner {
        //  Inner classes cannot declare static variables 
        //private static String innerName = "Inner Class";
        //  Internal classes can only declare instance variables and final Type static variable 
        private int a;
        final static int x = 1;
        //  Inner classes cannot declare static code blocks, enumeration classes, static inner classes, interfaces ( Enumeration types and interface types are always static )
        //static { }
        //enum InnerEnum { }
        //static class Inner2 { }
        //interface Inner3 { }
        //  Inner class declares inner class 
        class Inner4 { }
        //  Inner classes cannot declare static methods 
        //static void OperateStatic() { }
        //  Internal classes can only declare instance methods 
        void operate() {
            System.out.println(" The inner class accesses the static variables of the outer class: name = " + name);
            System.out.println(" Internal classes access the static state of peripheral classes final Variables: FLAG_VALUE = " + FLAG_VALUE);
            System.out.println(" The inner class accesses the instance variables of the peripheral class: age = " + (age = 40));
            System.out.println(" The inner class accesses the static methods of the peripheral class: getName() = " + getName());
            System.out.println(" The inner class accesses the instance methods of the peripheral class: getAge() = " + getAge());
        }// Join Java Develop and exchange samples: 7565848221 Blowing water and chatting 
    }
}

4. How do inner classes bind peripheral objects?

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


public class Outer {
    public class Inner {
        //  Member variables of peripheral class types automatically implicitly generated by the compiler 
        final Outer this$0;
        //  Pass the object of the peripheral class to the inner class constructor this$0 Member variable, which realizes the binding of inner class and peripheral class objects 
        public Inner(Outer outer) {
            this$0 = outer;
            super();
        }
    }
}

Create an internal class object, as follows:


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

When you create an inner class object, the system automatically passes the object of the outer class (outer) as a parameter into the constructor of the inner class, which can be considered as follows:


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

5. Inheritance

Access permissions permitting: An inner class can inherit from, or be inherited by, any class.

Q: What is the difference in inheritance between inner classes and static inner classes?

Answer: The objects of the inner class always depend on the outer objects, so if a class A inherits an inner class, the class A must also be bound with the outer class objects of the inner class, otherwise a compilation error will occur.

1. Counterexample: Generate a compilation error


public class A extends Outer.Inner { }

class Outer {
    class Inner { }
}

2. Error cause: Want to create an A class object, that is, A a = new A (); The A class constructor is called, and if the A class inherits the inner class (Outer. Inner), the constructor of the inner class is called

At this time, there is no valid peripheral class object, so it is impossible to bind the internal class object with the peripheral class object, resulting in compilation errors.

3. Amendments

Method 1: Pass a reference of peripheral class in A class constructor, and call the constructor of internal class through peripheral class object, which is equivalent to passing the object of peripheral class to the constructor of internal class, thus realizing the binding between internal class object and peripheral class object.


public class A extends Outer.Inner {
    public A(Outer outer) {
        outer.super();
    }
}
class Outer {
    class Inner { }
}

Method 2: External class inherits external class, and internal class inherits internal class


public class A extends Outer {
    class InnerA extends Outer.Inner { }
}
class Outer {
    class Inner { }
}

Create the inner class InnerA object of the A class:


A a = new A();
A.InnerA innerA = a.new InnerA();

When you create the inner class A. InnerA object, you need to bind the peripheral object, and the a reference is the object of the peripheral class.

The inner class A. InnerA inherits another inner class Outer. Inner. When A. InnerA calls the parent class constructor, it also needs to pass the peripheral class (Outer) object of the parent class.
The A class inherits the Outer class, because the object of the subclass can be used as the object of the parent class, so the 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 and initialization 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.


public class LocalInnerDemo {
    private int x = 100;
    // 1. The local inner class is declared in the instance initialization block 
    {
        class Local1 { }
    }
    // 2. Local inner classes are declared in static initialization blocks 
    static {
        class Local2 { }
    }
    // 3. Local inner classes are declared in the constructor 
    public LocalInnerDemo() {
        int y = 2;
        final int z = 3;
        class Local3 {
            int a = x;
            int b = y;
            int c = z;
        }
    }// Join Java Develop and exchange samples: 7565848221 Blowing water and chatting 
    // 4. Local inner classes are declared in instance methods 
    public T1 method1() {
        //  Implement an interface with a local inner class and return it as an interface 
        class Local4 implements T1 {
            @Override
            public void operate() {
                System.out.println("Start to operate.");
            }
        }
        return new Local4();

    }
    // 5. Local inner classes are declared in static methods 
    public static T1 method2() {
        class Local5 implements T1 {
            @Override
            public void operate() {
                System.out.println("Start to operate.");
            }
        }
        return new Local5();
    }
}
interface T1 {
    void operate();
}

Q: What is the difference between a local inner class declaration in an instance environment (instance method, constructor, instance initialization block) and a static environment?

Answer: Instance environment: The local inner class needs to be bound to the peripheral class, which implicitly generates a reference to final 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, there is no need to implement methods of nested interfaces.

Summarize

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: