The following declares the class to see the test results:
[Java]View PlainCopy
- Package Com.shareniu;
- Public class Helloa {
- {
- System.out.println ("before class Helloa");
- }
- Public Helloa () {
- System.out.println ("Helloa");
- }
- static{
- System.out.println ("static Helloa");
- }
- {
- System.out.println ("after class Helloa");
- }
- }
- Public class Hellob extends Helloa {
- private int i;
- Public Hellob () {
- System.out.println ("Hellob");
- }
- {
- System.out.println (i);
- System.out.println ("before class Hellob");
- }
- {
- System.out.println ("after class Hellob");
- }
- static{
- System.out.println ("static Hellob");
- }
- Public static void Main (string[] args) {
- New Hellob ();
- System.out.println ("Shareniu");
- New Hellob ();
- }
What is the result of the output?
[Java]View PlainCopy
- Static Helloa
- Static Hellob
- Before class Helloa
- After class Helloa
- Helloa
- 0
- Before class Hellob
- After class Hellob
- Hellob
- Shareniu
- Before class Helloa
- After class Helloa
- Helloa
- 0
- Before class Hellob
- After class Hellob
- Hellob
1.1.1.Summary
1. When instantiating a class object, the static method of the parent class is instantiated first, and then the static method of the subclass is instantiated and executed only once.
2. The code of the static block has the highest priority.
3. Initialize the code block of the parent class before executing the parent class's construction method. Continue to initialize your own block of code, and then execute your own construction method.
4. Code blocks are executed in favor of the construction method. This is the result of the 2-time construction of class object initialization. The code block initializes all of the variable information, and when the constructor is executed, the memory of the modified value is initialized again if there is a variable assignment.
5. Always load the parent class first, and the child class is loaded. Load the static variable information in the Loaded class object first.
6. The execution order of the code block {} is executed from top to bottom.
Java class loading mechanism (record only)