Java---class loading mechanism, constructor method, static variable, parent class, variable load order

Source: Internet
Author: User

Directly on the code:

Code Listing 1:

 Public classConstrocttest {Private StaticConstrocttest test =Newconstrocttest (); //static variable sta1 not given an initial value     Public Static intsta1; //static variable sta1 gives the initial value     Public Static intSta2 = 20; //assigning values to static variables in construction methods    Privateconstrocttest () {sta1++ ; Sta2++ ; }     Public Static voidMain (string[] args) {System.out.println (CONSTROCTTEST.STA1);    System.out.println (CONSTROCTTEST.STA2); }}

Results:

1
20

Code Listing 2:

 Public classConstrocttest {//static variable sta1 not given an initial value     Public Static intsta1; //static variable sta1 gives the initial value     Public Static intSta2 = 20; Private StaticConstrocttest test =Newconstrocttest (); //assigning values to static variables in construction methods    Privateconstrocttest () {sta1++ ; Sta2++ ; }     Public Static voidMain (string[] args) {System.out.println (CONSTROCTTEST.STA1);    System.out.println (CONSTROCTTEST.STA2); }}

Results:

1
21st

Results Analysis:

1. Initialize each static variable in the order of the static variables. (assigning default values to variables)

2. In order, give the initial value of the static variable.

3. The result is that the position of the class static variable determines whether the value given to sta1 and Sta2 is valid by constructing the method.

4. In code one, the STA2 + + operation is performed for STA2 first. The STA2 is then given a static variable value. (only because of the order problem)

Code Listing 3:

 Public classConstrocttest {//static variable sta1 not given an initial value     Public Static intsta1; //static variable sta1 gives the initial value     Public Static intSta2 = 20; Private StaticConstrocttest test =Newconstrocttest (); //assigning values to static variables in construction methods    Privateconstrocttest () {System.out.println ("123456"); Sta1++ ; Sta2++ ; }     Public Static voidMain (string[] args) {System.out.println (CONSTROCTTEST.STA1);        System.out.println (CONSTROCTTEST.STA2);        System.out.println (CONSTROCTTEST.STA1);    System.out.println (CONSTROCTTEST.STA1); }}

Results:

Results Analysis:

1. As you can see from the results, the static variables of Java are executed only when the class is first loaded and initialized.

2. Class variables do not depend on instances of classes, and class variables are allocated only once in the stack memory at initialization time, no matter how many instances of the class are created, no more space is allocated for class variables.

3. As you can see, the execution and initialization of class variables is not related to instance objects.

Code Listing 4:

 Public classtest{ Public Static voidMain (string[] args) {Child ch=NewChild (); }}classparent{StaticString name1 = "Hello"; Static{System.out.println ("Parent static Block"); }     PublicParent () {System.out.println ("Parent construct block"); }}classChildextendsparent{StaticString name2 = "Hello"; Static{System.out.println ("Child Static Block"); }     PublicChild () {System.out.println ("Child construct block"); }}

Results:

Results Analysis:

1. Initialize the static property of the parent class before executing its own static property, and then the constructor of the parent class is its own construction method.

2. Instantiate the child class. The first to initialize the class child, because child has a parent class (which determines whether the parent class is initialized), the class is initialized only once: Initializes the class (that is, static variables and static methods are loaded sequentially).

3. After the child is initialized. Begins instantiating a child because it has a parent class, so the default constructor method of the parent class is called before the constructor method is called.

Code Listing 5:

public class Animal {private static int k;static{system.out.println ("static method of the parent class");} {System.out.println ("to execute the construction code block of the parent class");} Public Animal () {System.out.println ("constructor method for executing the parent class");} public static void Main (string[] args) {System.out.println (ANIMAL.K);}}

Operation Result:

Static methods of the parent class
0

Results Analysis:
1. Constructing code blocks is not related to the loading of classes.

Code Listing 6:

 Public classAnimal {Private Static intK; {System.out.println ("Executing the construction code block of the parent class"); }    Static{System.out.println ("Static method of the parent class"); }     PublicAnimal () {System.out.println ("Constructor method for executing parent class"); }     Public Static voidMain (string[] args) {Animal animal1=NewAnimal (); Animal Animal2=NewAnimal (); }}

Results:

Results Analysis:

1. Construct the code block as to the construction method, as the construction method executes.

Code Listing 7:

 Public class Cat {    privatestaticint  A;     Private Static int b = +;     Static {        = +;         = $;    }          Public Static void Main (string[] args) {        System.out.println (CAT.A);        System.out.println (cat.b);    }}

Results Analysis:

1. You can think of the contents of a static code block as a given operation.

2. When the static code block is in front of a B. The result of this output is 100 1000

Code Listing 8:

classAnimal {{System.out.println ("Executing the construction code block of the parent class"); }    Static{System.out.println ("Static method of the parent class"); }     PublicAnimal () {System.out.println ("Constructor method for executing parent class"); }     Public voidA () {System.out.println ("A method of executing a parent class"); }  } Public classCatextendsanimal{Static{System.out.println ("Static methods for subclasses"); } {System.out.println ("Building code blocks for subclasses"); }     PublicCat () {System.out.println ("Constructor method for executing subclasses"); } @Override Public voidA () {System.out.println ("A method of executing a subclass"); }     Public Static voidMain (string[] args) {Cat C=NewCat ();    C.A (); }}

From the above analysis, the results should be clear:

Summary:1 The class is loaded first when you start an instance of new B. 2, when loading the class, first load the parent class A, and then load the subclass B
3, after loading the parent class A, complete the static action (including static code and variables, their levels are the same, initialization occurs in the installation code)
4, after loading sub-Class B, complete the static action
Class mount complete, start instantiation
1, when instantiating subclass B, first instantiate the parent class A
2, the member instantiation (non-static code) when the parent class A is instantiated
3, the construction method of parent Class A
4, member instantiation of subclass B (non-static code)
5, the construction method of sub-class B

initializes the static code of the parent class---> initializes the static code of the subclass--initializes the non-static code of the parent class---> initializes the parent class constructor---> Initialize subclass non-static code---> Initialize Subclass construction Letter

Java---class loading mechanism, constructor method, static variable, parent class, variable load order

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.