Class loading order for Java

Source: Internet
Author: User

Class loading order for Java

First, load order

1. Initialization of the static member variable of the parent class

1.1. Static code block

1.2, ordinary code block

1.3, no parameter constructor

2. Static code block for parent class

3. Initialization of a static member variable of a subclass

  3.1. Static code block

  3.2, ordinary code block

3.3, no parameter constructor

4. Static code blocks for subclasses

5. The normal member variable of the parent class is initialized

5.1. Static code block

5.2, ordinary code block

5.3, no parameter constructor

6. Normal code block for parent class

7. No parameter constructor for parent class

8. Normal member variables for subclasses

8.1. Static code block

8.2, ordinary code block

8.3, no parameter constructor

9. Generic code blocks for subclasses

10. Non-parametric constructors for sub-classes

Second, the sample code

Super Parent Class A, parent Class B, subclass c,b inherit a,c inherit B;

AA, AAA, BB, BBB, CC, CCC have their own common, static member variables, ordinary, static code block and no parameter constructor

Super Parent Class A's ordinary member variable AA (instance of AA Class), Super parent Class A static member variable AAA (instance of AAA Class);

The normal member variable BB (instance of the BB Class) of the parent class B, the static member variable BBB (an instance of the BBB class) of the parent class B;

Subclass C of the ordinary member variable CC (Instance of the CC class), subclass C static Member variable CCC (instance of the CCC class);

1. Super Parent Class A

Package cn.com.zfc.lesson05.inherit01;

/**
*
* @title A
* @describe Super Parent class A
* @author Zhang Fuchang
* @date April 3, 2017 5:59:17
*/
public class A {

Normal member variables

Private AA AA = new AA ();

Static member variables
private static AAA AAA = new AAA ();

Static code block
static {
System.out.println ("I am a static code block of Super Parent Class A");
}

Common code blocks
{
System.out.println ("I am a generic code block for Super parent Class A");
}

Non-parametric construction method
Public A () {
System.out.println ("I am a non-parametric construction method of Super Parent Class A");
}
}

2. Parent Class B

Package cn.com.zfc.lesson05.inherit01;

/**
*
* @title B
* @describe Parent class
* @author Zhang Fuchang
* @date April 3, 2017 5:59:17
*/
public class B extends A {

Normal member variables
private BB bb = new BB ();

Static member variables
private static BBB BBB = new BBB ();

Static code block
static {
System.out.println ("I am a static block of code for the parent class B");
}

Common code blocks
{
System.out.println ("I am a normal block of code for the parent class B");
}

Non-parametric construction method
Public B () {
System.out.println ("I am the non-parametric construction method of the parent class B");
}
}

3, sub-class C

Package cn.com.zfc.lesson05.inherit01;

/**
*
* @title C
* @describe Subclass C
* @author Zhang Fuchang
* @date April 3, 2017 5:59:17
*/
public class C extends B {

Normal member variables

private CC CC = new CC ();

Static member variables
private static CCC CCC = new CCC ();


Static code block
static {
System.out.println ("I am a static block of code for subclass C");
}

Common code blocks
{
System.out.println ("I am a generic code block of Subclass C");
}

Non-parametric construction method
Public C () {
System.out.println ("I am the non-parametric construction method of subclass C");
}
}

4, AA class

Package cn.com.zfc.lesson05.inherit01;

/**
*
* @title AA
* @describe the normal member variable AA of Super parent Class A
* @author Zhang Fuchang
* @date April 3, 2017 5:59:17
*/
public class AA {
Static code block
static {
System.out.println ("I am a static code block of the Super parent Class A's ordinary member variable AA");
}

Common code blocks
{
System.out.println ("I am the normal block of the super parent Class A's ordinary member variable AA");
}

Non-parametric construction method
Public AA () {
System.out.println ("I am the super parent Class A's ordinary member variable AA");
}
}

5. AAA Class

Package cn.com.zfc.lesson05.inherit01;

/**
*
* @title AAA
* @describe the static member variable AAA of Super parent Class A
* @author Zhang Fuchang
* @date April 3, 2017 5:59:17
*/
public class AAA {
Static code block
static {
System.out.println ("I am the static member variable AAA of the Super parent Class A static code block");
}

Common code blocks
{
System.out.println ("I am a static member of Super parent Class A variable AAA code block");
}

Non-parametric construction method
Public AAA () {
System.out.println ("I am the super parent Class A static member variable AAA of the non-parametric construction method");
}
}

6. BB Class

Package cn.com.zfc.lesson05.inherit01;

/**
*
* @title BB
* @describe the normal member variable BB of the parent class B
* @author Zhang Fuchang
* @date April 3, 2017 5:59:17
*/
public class BB {
Static code block
static {
System.out.println ("I am the static code block of the normal member variable BB of the parent class B");
}

Common code blocks
{
System.out.println ("I am the normal member variable BB of the parent class B's ordinary code block");
}

Non-parametric construction method
Public BB () {
System.out.println ("I am the normal member of the parent class B variable BB of the non-parametric construction method");
}
}

7. BBB Class

Package cn.com.zfc.lesson05.inherit01;

/**
*
* @title BBB
* @describe static member variable BBB for parent class B
* @author Zhang Fuchang
* @date April 3, 2017 5:59:17
*/
public class BBB {
Static code block
static {
System.out.println ("I am static code block of the static member variable BBB of the parent class B");
}

Common code blocks
{
System.out.println ("I am the normal code block of the static member variable BBB of the parent class B");
}

Non-parametric construction method
Public BBB () {
System.out.println ("I am the static member variable of the parent class B, the non-parametric construction method of BBB");
}
}

8. CC Class

Package cn.com.zfc.lesson05.inherit01;

/**
*
* @title CC
* @describe Subclass C's Ordinary member variable CC
* @author Zhang Fuchang
* @date April 3, 2017 5:59:17
*/
public class CC {
Static code block
static {
System.out.println ("I am a static code block of the normal member variable CC of subclass C");
}

Common code blocks
{
System.out.println ("I am a generic block of the normal member variable CC of subclass C");
}

Non-parametric construction method
Public CC () {
System.out.println ("I am the non-parametric constructor of the generic member variable CC of subclass C");
}
}

9. CCC Class

Package cn.com.zfc.lesson05.inherit01;

/**
*
* @title CCC
* @describe the static member variable of subclass C CCC
* @author Zhang Fuchang
* @date April 3, 2017 5:59:17
*/
public class CCC {
Static code block
static {
System.out.println ("I am the static member variable of the subclass C Static code block of CCC");
}

Common code blocks
{
System.out.println ("I am a generic code block of the static member variable CCC of subclass C");
}

Non-parametric construction method
Public CCC () {
System.out.println ("I am a static member variable of subclass C, the method of non-parametric construction of CCC");
}
}

10, test the creation process of subclass C: Testc.java

Package cn.com.zfc.lesson05.inherit01;
/**
*
* @title TESTC
* @describe test the creation process of subclass C
* @author Zhang Fuchang
* @date April 3, 2017 6:49:50
*/
public class TESTC {
public static void Main (string[] args) {

Create a C object
c C = new C ();
}
}

Third, test results

I'm a static block of static member variable AAA for Super parent Class A
I'm a static member variable of Super parent Class A normal code block of AAA
I'm a static member variable of Super Parent Class A method of non-parametric construction of AAA
I'm a static code block for Super parent Class A
I'm a static block of static member variable BBB for the parent class B
I'm the static member variable of the parent class B. General code block of BBB
I'm the static member variable of the parent class B. The method of non-parametric construction of BBB
I'm a static block of code for the parent class B.
I'm the static member variable of subclass C, CCC static code block
I'm a subclass C static member variable CCC common code block
I'm the static member variable of subclass C. The method of non-parametric construction of CCC
I'm a static code block for subclass C.
I'm a static code block of the Super parent Class A's normal member variable AA
I'm a normal member of Super parent Class A. Variable AA ordinary code block
I'm an ordinary member of the super parent Class A. A method of non-parametric construction of a variable AA
I'm an ordinary block of code for super-parent Class A.
I'm a non-parametric construction method of Super parent Class A
I am the normal member of the parent class B variable BB static code block
I am the normal member of the parent class B variable BB common code block
I'm the normal member of the parent class B. A method of non-parametric construction of BB
I'm a normal block of code for the parent class B.
I'm the non-parametric construction method of the parent class B
I'm a static code block of the normal member variable CC of subclass C
I'm a normal member of subclass C. Variable CC common code block
I'm a subclass C. The non-parametric construction method of the ordinary member variable CC
I'm a generic code block for subclass C.
I'm the non-parametric construction method of subclass C

Summarize:

1th, all classes will load the base class first
2nd, initialization of static members takes precedence
3rd, the constructor method is not executed until the member is initialized
4th, the initialization of static members and the execution of static blocks occur at the time of class loading.
4th, the creation of class objects and access to static blocks will trigger the loading of classes.

Class load order for Java

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.