Java: constructor and code block (with or without static code), sub-class parent class, detailed explanation of dependency class execution sequence, javastatic

Source: Internet
Author: User

Java: constructor and code block (with or without static code), sub-class parent class, detailed explanation of dependency class execution sequence, javastatic

1. Dependency class members are executed first

2. The static code block (static) is prior to the normal code block.

3. the same type of code block (both static or non-static), parent class prior to subclass

Conclusion: [dependency class members]

-> Static block)

-> Static block)

-> Normal block)

-> Parent class Constructor

-> Subclass normal member (normal block)

-> Subclass Constructor

 

Example:

1/* 2 academic questions: 3. Analyze the execution sequence of the following code-> study the constructor and whether there is static 4-> subclass and parent class 5-> Self class and dependency Class 6 7. output results: 8 ---------- java ---------- 9 Subclass static block // ConstructorStaticInitDemo class (executed later) depends on Subclass class (executed first) 10 Superclass [static] block // code block without static modification execute 11 Superclass constructor 12 Subclass constructor 13 constructor 14 static block15 main16 17 18 */19 20 public class ConstructorStaticBlockDemo21 {22 private static ConstructorStaticBlockDemo constructorStaticBlockDemo = new ConstructorStaticBlockDemo (); 23 private Subclass subclass = new Subclass (); 24 25 static 26 {27 System. out. println ("static block"); 28} 29 30 public ConstructorStaticBlockDemo () 31 {32 System. out. println ("constructor"); 33} 34 35 public static void main (String [] args) 36 {37 System. out. println ("main"); 38} 39} 40 41 class Superclass42 {43 Superclass () 44 {45 //-> execute code block 46 System without static modification. out. println ("Superclass constructor"); 47} 48 49 // If static exists, the parent class code block is executed first (after the Superclass [static] block is executed, run Subclass static block) 50 {51 System. out. println ("Superclass [static] block"); 52} 53} 54 55 56 class Subclass extends Superclass57 {58 59 Subclass () 60 {61 // implicit super (); 62 System. out. println ("Subclass constructor"); 63} 64 65/static code blocks are executed when the bytecode is loaded into JVM, that is, execute 66 static 67 {68 System as the class is executed. out. println ("Subclass static block"); 69} 70 71}

 

Decompile the class bytecode:

1 public class Consumer 2 {3 4 private static ConstructorStaticBlockDemo = new consumer (); 5 private Subclass subclass; 6 7 public constructorStaticBlockDemo () 8 {9 subclass = new Subclass (); 10 System. out. println ("constructor"); 11} 12 13 public static void main (String args []) 14 {15 System. out. println ("main"); 16} 17 18 static 19 {20 System. out. println ("static block"); 21} 22}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.