What is the static inner class of Android interview questions? What is the difference between non-static inner classes and non-static inner classes?

What are static inner classes? What is the difference between non-static inner classes and non-static inner classes?

What is this question trying to investigate?

Master the function and precautions of static

Knowledge points of inspection

The keyword static in Java

How should candidates answer

When defining an inner class, if the inner class is declared static, the inner class is a static inner class.

public class OuterClass{
    
    
    static class InnerClass{
    
    
        
    }
}

When the inner class is declared static, then the properties of the outer class cannot be directly used in the inner class. For example, to write an ordinary inner class:

public class OuterClass{
    
    
    int i;
    public class InnerClass{
    
    
        public InnerClass(){
    
    
            i = 10;
        }
    }
}

At this point, compile OuterClass.java and generate two files: OuterClass.class and OuterClass$InnerClass.class. Decompiling the latter we see:

public class OuterClass$InnerClass {
    
    
    public OuterClass$InnerClass(OuterClass var1) {
    
    
        this.this$0 = var1;
        var1.i = 10;
    }
}

It can be seen that the ordinary inner class construction method actually implicitly transfers the outer class instance object to the inner class. Use the properties of the outer class in the inner class: i. In fact, it is obtained through the instance object of the external class: var1. At the same time, if we need to construct InnerClassan instance object, the non-static inner class cannot be created without the outer class entity.

Below we define InnerClass as a static static inner class:

public class OuterClass{
    
    
    int i;
    public static class InnerClass{
    
    
        public InnerClass(){
    
    
            //i = 10; 
        }
    }
}

The ordinary member properties of the outer class cannot be used at this time: i. Its corresponding bytecode is:

public class OuterClass$InnerClass {
    
    
    public OuterClass$InnerClass() {
    
    
       
    }
}

The instance object of the outer class is no longer implicitly held in the static inner class. But if we define the attribute i as static, then the static member attributes of the outer class can also be directly used in the static inner class. At this time, the bytecode is:

public class OuterClass$InnerClass {
    
    
    public OuterClass$InnerClass() {
    
    
        OuterClass.i = 10;
    }
}

Inner static classes do not need to have references to outer classes. But the non-static inner class needs to hold a reference to the outer class. But static inner classes can be new OuterClass.InnerClass()instantiated directly using .

Therefore, the differences between static inner classes and non-static inner classes are:

  1. Non-static inner classes can access both static and non-static members of the outer class, and static classes can only access static members of the outer class.
  2. Non-static inner classes cannot be created without the outer class, static inner classes can.

2.4 When passing parameters in Java, is the value passed or the reference passed?

What is this question trying to investigate?

Do you know what pass by value and pass by reference are used in real scenarios, and are you familiar with what is pass by value and pass by reference and what are their performances at work?

Knowledge points of inspection

What are the concepts of pass by value and pass by reference, and their impact on the code written in development

How should candidates answer

Value transfer: When the method is called, the passed parameter is a copy of the value pointed to by this parameter;

Pass by reference: when the method is called, the address of the reference is passed

The passing of parameters in Java can be divided into two situations:

1. Parameters of basic data types

 1 public class TransferTest {
    
    
 2     public static void main(String[] args) {
    
    
 3         int num = 1;
 4         System.out.println("changeNum()方法调用之前:num = " + num);
 5         changeNum(num);
 6         System.out.println("changeNum()方法调用之后:num = " + num);
 7     }
 8 
 9     public static void changeNum(int x) {
    
    
10         x = 2;
11     }
12 }

operation result:

img

The schematic diagram of the transfer process is as follows:

img

Analysis: When num is passed as a parameter to the changeNum() method, a copy of the value 1 stored in the storage unit pointed to by num in the memory space is copied and passed to the x variable in the changeNum() method, and this x variable A unit of storage that is also allocated in memory space. At this time, the value 1 of the num pair is passed to the storage unit pointed to by the x variable. After that, all operations on the x variable in the changeNum() method are aimed at the storage unit pointed to by x, regardless of the storage unit pointed to by num.

Therefore, after the changeNum() method is called, the value of the storage unit pointed to by num still does not change, which is the so-called "value transfer".

The essence of value transfer is: what is passed is the content in the storage unit, not the reference of the storage unit.

2. Parameters of reference type

 1  public class TransferTest2 {
    
    
 2     public static void main(String[] args) {
    
    
 3         Person person = new Person();
 4         System.out.println(person);
 5         change(person);
 6         System.out.println(person);
 7     }
 8 
 9     public static void change(Person p) {
    
    
10         p = new Person();
11     }
12 }
13 
14 /**
15  * Person类
16  */
17 class Person {
    
    
18 
19 }

operation result:

img

It can be seen that the two print results are consistent. That is, after calling the change() method, the person variable has not changed.

The schematic diagram of the transfer process is as follows:

img

analyze:

01. When the program executes to the third line Person person = new Person(), the program opens up a memory space in the heap memory (heap) to store the instance object of the Person class, and at the same time opens up a memory space in the stack memory (stack). The storage unit is used to store the reference of the instance object, that is, the storage unit pointed to by person in the above figure.

02. When the program executes to line 5 change(person), person is passed to the change() method as a parameter (actual parameter). Here the person passes the contents of his storage unit to the p variable of the change() method. After that, all operations on the p variable in the change() method are aimed at the storage unit pointed to by the p variable, and have nothing to do with the storage unit pointed to by perosn.

Therefore, Java's parameter passing, whether it is a parameter of a basic data type or a reference type, is passed by value!

More detailed explanations of interview questions can be obtained for free by scanning the QR code!

Guess you like

Origin blog.csdn.net/Misdirection_XG/article/details/131047760