6. Member Initialization
Java tries to ensure that all variables are properly initialized before they can be used. For local variables of the method, Java implements this assurance in the form of compilation errors.
If the class has a member variable when the base type is not initialized to the default value
When you define a reference object in a class, if you do not initialize it, the reference obtains a special value of NULL
7. Constructor initialization
The constructor can be used for initialization. Keep in mind, however, that automatic initialization cannot be prevented, and it will occur before the constructor is called.
Order of initialization
The order in which the variables are defined determines the order in which they are initialized. Even if the variable definitions are scattered between the method definitions, they will still be initialized between any methods (including constructors) that are called.
Initialization of static data
Static data consumes only one copy of the storage area, regardless of how many objects are created. The static keyword cannot be applied to local variables, so it can only be used for fields. If a domain is a static base type field and is not initialized, then it gets the standard initial value of the base data type, and if it is a reference data type, its default initial value is null.
Static initialization occurs only at the necessary time. If you do not create a Table object and do not reference TABLE.B1 or TABLE.B2, then static bowl B1 and B2 will never be created. They are initialized only when the first table object is created (or when static data is accessed for the first time).
The order of initialization is a static object, followed by a non-static object
Summarize the object creation process, assuming there is a class named Dog.
1, even if the static keyword is not explicitly used, the constructor is actually a static method. Therefore, the Java parser must look for the classpath to locate the Dog.class file when first creating an object of type dog or a static method/static domain for the dog is accessed for the first time.
2. Then load Dog.class, and all actions related to static initialization will be executed. Therefore, static initialization is performed only once when the class object is first loaded
3. When creating an object with new dog, first allocate enough storage space for the dog object on the heap
4. This storage space is zeroed, which automatically sets all the basic types in the dog object to the default values, and the reference is set to NULL
5. Perform all initialization operations that appear in the field definition
6. Execute the constructor
An explicit static initialization
Java allows multiple static initialization actions to be organized into a special "static clause", sometimes called a "static code block". Although it looks like a method, it's actually just a piece of code that follows the static keyword. As with other static initialization actions, this code executes only once: When an object of this class is first generated, or when a static data member belonging to that class is first accessed.
Non-lectern instance initialization
Java also has a similar syntax called instance initialization, which is used to initialize non-static variables for each object.
They are exactly the same as static initialization clauses, except the static keyword is missing. This syntax is necessary to support initialization of anonymous inner classes, but it also allows you to guarantee that some operations will occur regardless of which explicit constructor is called. The instance initialization clause is executed before the constructor.
Initialization of Java (programming idea)