java running order, summary of notes, basic concepts

Java running order, summary of precautions, summary of basic concepts


:
1. Static variables are variables that belong to the entire class rather than to an object.
2. Be careful not to declare variables in any method body as static (declaring static variables in the method body will report an error)
3. In Java, classes are loaded on demand, and this class will be loaded only when it is needed. And it will only be loaded once
4. In the process of generating an object, the member variables of the object will be initialized first, and then the constructor will be executed.
5. Both static variables and static name blocks are done when the class is loaded, not when the object is generated
6. When accessing static constants, if the compiler can calculate the value of the constant, the class will not be loaded. That is, if the static constant value of class A is assigned by the static constant of class B, it will not be loaded, otherwise class A needs to be loaded.
7. To access static constants, if the compiler can calculate the value of the constant (that is, the value will not change), the class will not be loaded, otherwise the class will be loaded
8. Create an object reference without loading the class. (One class defines another A reference to a class), a reference is to define a
variable Overridden (overridden), if the subclass overrides a method in the parent class, then when this method is called, the method in the subclass will be called 12. When using the parent class type to refer to the subclass object (Only the methods and variables of the parent class can be called, but the new methods and variables of the child class will not work), the reference calls the methods and variables defined in the parent class Object generation: class loading -> initialize static member variables -> execute Static statement block -> initialize object's member variables -> execute non-static statement block -> execute constructor to generate object









Parent class static member variable -> parent class static statement block -> child class static member variable -> child class static statement block ->
parent class non-static member variable -> parent class non-static statement block -> parent class constructor -> Subclass non-static member variable -> subclass non-static statement block -> subclass constructor

Parent class initialization -> parent class constructor -> subclass initialization -> subclass constructor



Object initialization:
1) For char, short, Variables of basic data types such as byte, int, long, float, and double are initialized to 0 by default (boolean variables are initialized to false by default);
2) For variables of reference type, they are initialized to null by default. String default value is null
3) If the constructor is not explicitly defined, the compiler will automatically create a no-argument constructor, but keep in mind that if the constructor is explicitly defined, the compiler will not automatically add the constructor device.



Java class loading sequence
1. First load the class, perform static variable initialization, and then perform object creation 2. The java
virtual machine requires that all static variables must be completed before the object is created 3. Java
constants are placed in the memory constant pool, its The mechanism is different from that of variables. When compiling, loading constants does not require loading a class.



Initialization
sequence: If an object of a certain class needs to be generated, the Java execution engine will first check whether the class is loaded. If it is not loaded,
the class will be executed first. Load and then generate the object, if already loaded, directly generate the object.

During the loading process of the class, the static member variables of the class will be initialized. If there is a static statement block in the class, the static statement block will be
executed . The execution order of static member variables and static statement blocks is the same as the order in the code.



When the subclass calls the static method of the superclass
(1) When the subclass does not override the static method of the superclass, only the superclass is loaded, not the subclass
(2) When the subclass has a static method that overrides the superclass, it is loaded both. The parent class, and the execution order of the subclass



static{} statement block
(1) When there are multiple static{} in a class, according to the definition order of static{}, execute from front to back; (execute in writing order)
( 2) After the content of the static{} statement block is executed, the main call statement will be executed;



Inheritance:
The subclass inherits the method of the parent class (static, non-static) The
subclass inherits the member variables of the parent class (static, non-static) Static)
subclasses do not inherit the constructor



Hidden
: subclasses inherit the member variables of the parent class, but cannot inherit the private member variables of the parent class. If a member variable with the same name appears in the subclass, a hidden phenomenon will occur,
that is , the subclass's The member variable will block the member variable of the same name of the parent class. If you want to access the member variable of the same name in the parent class in the subclass, you need to use the super keyword to refer to it.
Both static methods and static variables will be hidden



. Override
: The subclass inherits the method of the parent class, but cannot inherit the private member method of the parent class. If a member method with the same name appears in the subclass, it is called overriding,
that is, the member method of the subclass. It will override the member method of the same name of the parent class. If you want to access the member method of the same name in the parent class in the subclass, you need to use the super keyword to refer to it.



Understanding of hiding and overriding:
hiding is for member variables and static methods, while overriding is for ordinary methods (so member variables (static, non
- methods call your reference variable type)

Overriding is only for non-static methods, that is to say, only the overriding method will be dynamically bound, and the hidden method will not be dynamically bound.
In Java, except static methods and final methods, all other methods are dynamically bound



to this and super cannot be used in static methods:
this and super cannot be used in static methods, this is a reference to the current object, and super is Refers to a reference to an object of the parent class.
If there are this and super in the static method, then when the static method is loaded into the memory, the this and super are also loaded into the memory,
but the object has not been created this and super has not been initialized, so an error will be reported when loading.



Constructor:
If the constructors of the parent class have parameters, you must explicitly call the constructor of the parent class through the super keyword in the constructor of the child class with the appropriate parameter list.

If parent class has or not If the super keyword is not used, the
system will automatically call the no-parameter constructor of the parent class.



Overriding (Overriding) and overloading (Overloading) understanding:
If a method is defined in a subclass with the same name and parameters as its parent class, we say the method is overriding (Overriding).
When an object of the subclass uses this method, the definition in the subclass will be called, and for it, the definition in the superclass is "masked".

If multiple methods with the same name are defined in a class, and they either have different parameter numbers or different parameter types, it is called method overloading.
The Overloaded method can change the type of the return value but at the same time the parameter list has to be different.



Interfaces and abstract classes:
The interface is public and cannot have private methods or variables in it. It is for others to use, while abstract classes can have private methods or private variables. Those who

implement the interface must implement all the methods defined in the interface, and implement Abstract classes can selectively override the methods that need to be used.
In general applications, the top level is the interface, then the abstract class implements the interface, and finally the concrete class implements it.

An abstract class is a class that has one or more abstract methods and must be declared abstract. The characteristic of abstract classes is that instances cannot be created.



Priority of method calls: The
priority from high to low is: this.show(O), super.show(O), this.show((super)O), super.show((super)O).

First, check if there is any in this class, then look in the parent class, if you find it, see if this class has been rewritten, and don't look at the parameter changed to the parent class (one level up to the parent class) to see if there is a
matching method , and then the parameter becomes the parent class to find out if there is any (processed in this order)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326991725&siteId=291194637