This section elaborates on one side of all the code blocks involved in the initialization of classes and objects.
Example code:
1 Public classDemo {2 Private StaticString name;3 PrivateString age;4 5 //Main function6 Public Static voidMain (string[] args) {7Demo Demo =NewDemo ();8 9 }Ten One //constructor Function A PublicDemo () { -System.out.println ("I am a constructor that runs as the object is built"); - } the - //I'm building blocks of code . - { -System.out.println ("I'm building a block of code that runs as the object builds, takes precedence over the object's constructor"); + - } + //I am a static block of code A Static { atSystem.out.println ("I am a static block of code that runs as the class loads"); - } -}
Create a new demo class, the main function of the new such object demo;
To run the steps:
While the 1.Demo class loads, the static code blocks are loaded and executed together, and the function is to initialize the class
The 2.new keyword creates space in the heap, allocates memory addresses, and establishes unique properties for the object. The default initialization is performed, and the properties are displayed initialized.
3. Initialization of the construction code block takes precedence over the constructor execution, which is initialized for all objects;
4. Following the execution of the constructor function, the function is to initialize the related object;
Java Basics-Initialization of classes and objects