People who interview Xiaomi know that xiaomi interviewers like to ask internal questions:

What is the difference between a static inner class and a non-static inner class?

A: Non-static inner classes can access the methods and properties of the outer class, but static inner classes cannot.

2. Why can non-static inner classes access methods and properties of outer classes?

A: Because a non-static inner class holds a reference to an external class object.

3. How does a non-static inner class hold a reference to an external class object?

A: Muddled

Most programmers can answer the second question, but the third question may be a problem.

Take a look at the Demo:

public class InnerDemo { public void main() { new InnerClass("lxx", 1); new StaticInnerClass("lxx"); } private class InnerClass { String name; public InnerClass(String temp, int l) { super(); } public void setName(String name) { } } public static class StaticInnerClass { private String name; public StaticInnerClass(String name) { super(); }}}Copy the code

Mutate the above Java files into class files, generating a total of 3 class files, respectively:

  • InnerDemo.class
  • InnerDemo$InnerClass.class
  • InnerDemo$StaticInnerClass.class

Take a look at each of these classes. (javap -v InnerDemo.class)

InnerDemo’s main method:

There are only two lines of code in the Main method, one for each of the two boxes in the figure. If you look at the constructor that creates the InnerClass and StaticInnerClass, you’ll notice that the InnerClass constructor takes an extra parameter to the InnerDemo (and places it in the first place). So when we create the InnerClass object, we already pass in the object of the outer class, so the non-static InnerClass can access the methods and properties of the outer class.

InnerDemo$innerclass. class

As you can see from this figure, the compiler does modify the constructor of the inner class by adding the parameters of the outer class in front of it.

So somebody said, what if I change my Java code to look like this? Does the compiler add an InnerDemo parameter? Answer: Yes. The compiler does not care if your own constructor has arguments to an external class, the compiler simply adds them to you.

private class InnerClass {
    String name;

    public InnerClass(InnerDemo innerDemo,String temp, int l) {
        super();
    }

    public void setName(String name) { }
}
Copy the code

If you look at the InnerClass diagram, we have two properties, one is name, which we defined ourselves in our Java code, and the other is this0, which the compiler created, so this0, this is what the compiler created, this is what the compiler created, So this0’s flag reads ACC_SYNTHETIC (this modifier means that this code is not in Java, but is generated by the compiler itself).

Finally, take a look at the StaticInnerClass content:

You can see that the constructor of the static inner class remains the same, with only the parameters defined in Java.

Look at this, you’re out of school.