No. 2 object and memory control (memory allocation), No. 2 object
1. instance variables and class variables
Member variables VS local variables
- Local variables (stored in method stack memory)
- Parameter: defined in the method signature. It is assigned by the method caller and disappears as the method ends.
- Local variables in the method: defined in the method. Display initialization must be performed in the method. After initialization, the local variables take effect and die as the method ends.
- Local variables in a code block: defined in a code block. It must be displayed and initialized in the code block. After initialization, the local variables take effect and die as the code block ends.
- Member variables (defined in the class body)
- No static: non-static variables/instance variables; static: static variables/class variables
- Static
- From the program perspective, the role of static is as follows:Change instance members to class members. A member of a class without static modification. The member belongs to the class instance.Class itself. Therefore, static can only modify the members of a class.
- Legal forward reference when defining member variables
- The initialization time of class variables is always before the initialization time of instance variables.
1 public class RightDef {2 // the following code is completely correct. 3 int num1 = num2 + 20; 4 static int num2 = 10; 5}
Public class RightDef {2 // invalid forward Reference 3 int num1 = num2 + 20; 4 int num2 = 10; 5}
Public class RightDef {2 // invalid forward Reference 3 static int num1 = num2 + 20; 4 static int num2 = 10; 5}
- In the same JVM, each Class has only one Class Object (this Class object can be obtained through reflection), but multiple objects can be created.
- Java allows Class variables to be passed through the Class instance object, even though Class variables belong to Class objects (the instance object does not have Class variables). The underlying layer is: Convert to Class variables through Class objects.
- Instance variable initialization time
- Class variable initialization time. There are only two types: specified when class variables are defined; static initialization blocks. The two are executed in the source code order. After the execution, execute another
- Class variable Initialization is divided into two stages;
- Phase 1: allocate memory and have default initialization values at this time (before the initial value is assigned, the instance variable is before the construction method, and the class variable is before the static code block)
- Stage 2: assign an initial value (essentially in a static code block)
2. parent class Constructor
- Implicit call and explicit call
- When a program creates a java object, the system always calls the initialization operation of the top-level parent class (including the initialization block and </Prior to> constructor ), next, call the initialization operations of all parent classes sequentially, and finally execute the initialization operations of this class to return the object instances of this class.
- Which constructor of the parent class is called first?
- ① Super shows calling the parent class Constructor
- ② This calls this class to overload the constructor, and then transfers it to the scenario ①
- ③ Neither super nor this is displayed. Before executing the subclass constructor, the system implicitly calls the non-argument constructor of the parent class.
- Note: super/this is used to call the constructor (mine: the constructor only needs to be constructed once, and other constructor should be constructed first ). It can only be used in the constructor and must be used as the first line of code. Only one (not available at the same time) can be called once.
- Instance variables for accessing subclass objects
- Before executing the constructor code, the memory occupied by the object has been allocated. The default value in the memory is null. The constructor only assigns an initial value.
- When the compilation type and running type of a variable are different and the instance variables of the referenced object are accessed through the variable, the value of the instance variable is determined by the type of the variable declared (the compilation type ); however, when accessing a method, it is determined by the actually referenced object (running type)
- Method for overwriting the access quilt class
- Avoid calling the method overwritten by the quilt class in the constructor of the parent class.Otherwise, during actual operation, the constructor calls the method that is rewritten by the subclass rather than the method of the parent class.
- Because: If the sub-class object instance is created through the sub-class constructor, if this constructor of the parent class is calledThe subclass Rewriting Method is executed before all the code of the subclass constructor is executed.As a result, the subclass rewriting method cannot access the instance variable of the subclass.
3. Memory Control for parent and child instances
- Differences Between Inherited member variables and inherited methods
- For a variable of the reference type, When you access the instance variable of the referenced object through this variable, the value of this instance variable depends onStatementThe type of the variable. When the method of the referenced object is called through the variable, the behavior of the method depends onActualType of the referenced object
- Why: the compiler transfers the inherited method to the subclass, but does not transfer the inherited member variables to the subclass. Therefore, the override method completely overwrites the method of the parent class, the instance variables cannot overwrite the member variables of the parent class. The subclass allows instance variables with the same name as the parent class.
- Sub-class instances in memory
- There is no parent class object instance in the system, and only a subclass object instance is available. However, all instance variables defined by all its parent classes are saved in the subclass object instance, so you can name them again.
- When a program creates a subclass object, the system not only allocates memory for the instance variables defined in this class, but also allocates memory for all instance variables defined in its parent class, even if the parent and child classes have duplicate names (Child classes will hide instance variables with the same name in the parent class, but will not completely overwrite them)
- The super keyword itself does not reference any object, and cannot be used as a real referenced variable (LimitationFunction)
-
- The subclass method cannot directly use return super; however, return this can be used directly to put back the called object.
- The program cannot directly use super as a variable, for example, judging super = a; this statement will cause compilation errors.
- Super statement: to access the hidden instance variables defined in the parent class/to call the override methods defined in the parent class, you can use super.LimitationTo modify the instance variables and methods.
- Class variables of parent and child classes
- Super. As a limitation to access the class variables defined in the parent class (you can also directly use the parent class name. attribute value <recommended, good readability>)
4. final Modifier
- Final modified variable
- The normal instance is highlighted and has a default initial value, but the final modifier must display the assigned initialization value
- In essence, final instance variables can only be displayed in the constructor
- In essence, final variables can only be assigned values in static initialization blocks.
- The final-modified local variable must be assigned a value and cannot be modified.
- Execute"Macro replacement"Variable
- For a final modified variable (whether it is a class variable, instance variable, or local variable), if the initial value is specified when the final variable is defined,AndThis initial value can be determined after compilation (direct amount, basic arithmetic expression, String concatenation <including implicit conversion, but explicit conversion is not allowed> <No access variable, no call Method> <The compiler is stupid and cannot be called (variable/method/...)>, so this final variable is essentiallyWill no longer be a variableIs equivalent to a direct volume. (It can be verified by the "=" method)
- That is,"Macro variable ", the compiler will directly replace the method that uses this variable in the program with the value of the change volume..
- Final variables only have the "macro variable" effect when the initial value is specified when the variable is defined. They are not specified elsewhere (they are not unrelated to the compilation)
- The final method cannot be overwritten.
- If a method (such as a qualifier) in the parent class cannot be accessed in the subclass, the method with the same name defined in the subclass does not overwrite the method in the parent class (@ override verification ), it's just a common method, so there's no final.
- Local variables in the internal class
- Local internal class <method body/code block> (including anonymous internal class) access Local variables in the method body (other internal classes <static/non-static internal classes> cannot access local variables in the method body), must be modified using final
- Why: The implicit "closure" generated by the local internal class will make the local variable exists without the method in which it is located (extended scope)
- Internal classes may expand the scope of local variables (eg. create a thread in the internal class and call a local variable in the new thread). To avoid the great confusion caused by the arbitrary modification of the local variable value after the execution of the method where the local variable is located, the compiler requires that all local variables accessed by internal classes must be modified using final.
- Roar