In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the initialization order of the Java class". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the initialization order of the Java class".
Initialization order of the Java class
It is useless to say more, the following is the code I have tested myself, which is clear at a glance:
1 package test1; 2 3 public class Test {4 public static void main (String [] argc) {5 new Child (); 6 System.out.println ("= ="); 7 new Child (); 8} 9} 10 11 class Child extends Parent {12 private static String staticField = getStaticField (); 13 static {14 System.out.println ("child static method block initialization"); 15} 16 private String field = getField () 17 {18 System.out.println ("child common method block initialization"); 19} 20 21 public Child () {22 System.out.println ("child constructor initialization"); 23} 24 25 public static String getStaticField () {26 System.out.println ("child static attribute initialization"); 27 return "staticField" 28} 29 30 public static String getField () {31 System.out.println ("child normal property initialization"); 32 return "field"; 33} 34} 35 36 class Parent {37 private static String staticField = getStaticField (); 38 static {39 System.out.println ("parent static method Block initialization"); 40} 41 private String field = getField () 42 {43 System.out.println ("parent common method block initialization"); 44} 45 46 public Parent () {47 System.out.println ("parent constructor initialization"); 48} 49 50 public static String getStaticField () {51 System.out.println ("parent static attribute initialization"); 52 return "staticField" 53} 54 55 public static String getField () {56 System.out.println ("parent General property initialization"); 57 return "field"; 58} 59}
Print the results:
1 parent static attribute initialization 2 parent static method block initialization 3 child static method block initialization 4 child static method block initialization 5 parent normal attribute initialization 6 parent common method block initialization 7 parent constructor initialization 8 child general attribute initialization 9 child common method block initialization 10 child constructor initialization 11 = = 12 parent general attribute initialization 13 parent common method block initialization 14 parent constructor Initialization 15 child general attribute initialization 16 child common method block initialization 17 child constructor initialization
After replacing the declaration order of static attributes and static initialization blocks, it is found that static attributes are related to the initialization order and declaration order of static initialization blocks. Similarly, general attributes are related to the initialization order and declaration order of ordinary initialization blocks.
Summary:
When a class meets the conditions for initialization (there will be a summary in a later blog), the parent class is initialized first (down from the top-level parent class Object), and then the subclass is initialized
When initializing the class, initialize static properties and static initialization blocks first (related to the declaration order), initializing down from the top-level parent class Object
When creating an object through new, initialize the normal properties and the normal initialization block (related to the declaration order) first, and then call the constructor, which is also executed downward from the top-level parent class Object
Static properties and static initialization blocks are initialized only once, and after the class is initialized, the object is created again through new, and only step 3 is repeated.
Next, I quote a sentence from Java's programming idea:
Within the class, the order of variable definitions determines the order of initialization, and even if variable definitions are scattered between method definitions, they are initialized before any methods, including constructors, are called.
Thank you for reading, the above is "what is the initialization order of the Java class", after the study of this article, I believe you have a deeper understanding of what the initialization order of the Java class is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.