In this chapter we will discuss the initialization and loading of classes.
Simply put, the class is loaded when the static member is accessed.
Let's take a more complex example and then explain the order of loading and initialization.
Package Com.ray.ch06;public class Sub extends Father {private int b = Print ("b");//5private static int c = Print ("C");// 1public Sub () {SYSTEM.OUT.PRINTLN ("Create sub"),//6}static {System.out.println ("sub Begin");//2}public static void Main (string[] args) {new Sub ();}} Class Father {private int a = print ("a");//3public static int print (String s) {System.out.println (s); System.out.println ("Father print"); return 0;} Public Father () {//4system.out.println ("Create Father");}}
Output:
C
Father Print
Sub Begin
A
Father Print
Create Father
B
Father Print
Create Sub
Execution order:
1. Execute the static part inside the Sub, including the domain and code block (Sub.class is loaded when the static property is accessed)
2. Execute the static part of the father, including domains and blocks of code
3. Execute the Father constructor
4. Execute sub Normal domain
5. Execute the Sub constructor
Note: In this case, the static part of the sub, the attribute domain and the code block are executed in the order of the Code, and there is no need to contact.
Summary: This chapter mainly discusses the loading and initialization sequence of classes.
This chapter is here, thank you.
-----------------------------------
Directory
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Understanding java-6.7 initialization and class loading from the beginning