Java class loading sequence and Java class loading sequence

Source: Internet
Author: User

Java class loading sequence and Java class loading sequence

Problem

Yesterday, someone asked me about the loading sequence of static methods, static code blocks, common code blocks, constructors, common methods, and static methods in a class? If there is something like inheriting this class, and there is also a method above, what is the loading order?

Instance 1

Normal method StaticVar

Package org. andy. test;/*** @ author andy * @ version: 10:52:32 ***/public class StaticVar {public StaticVar () {System. out. println ("---- StaticVar -----");}}

Test the Loading Method OrderLoad
Package org. andy. test;/*** @ author andy * @ version: 10:33:48 ***/public class OrderLoad {static {System. out. println ("---- OrderLoad static code block -----");} private static StaticVar staticVar = new StaticVar (); {System. out. println ("---- OrderLoad code block -----");} public OrderLoad () {System. out. println ("---- OrderLoad constructor -----"); System. out. println ("");} public static void staticFunction () {System. out. println ("---- OrderLoad static method -----");} public void normalFunction () {System. out. println ("---- OrderLoad normal method -----");}/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stubOrderLoad staticVar = new OrderLoad (); OrderLoad. staticFunction (); staticVar. normalFunction ();}}

The running result is as follows:


The loading sequence is:

Static code block ----- static method -------- common code block ------- constructor --------- call method (including static and non-static methods, in the call order)


Instance 2

Sub-class OrderLoadChild implements the parent class OrderLoad

Package org. andy. test;/*** @ author andy * @ version: 11:33:51 ***/public class OrderLoadChild extends OrderLoad {static {System. out. println ("---- OrderLoadChild static code block -----");} private static StaticVar staticVar = new StaticVar (); {System. out. println ("---- OrderLoadChild code block -----");} public OrderLoadChild () {System. out. println ("---- OrderLoadChild constructor -----"); System. out. println ("");}/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stubOrderLoadChild orderLoadChild = new OrderLoadChild ();}}

The running result is as follows:


The loading sequence is:

Parent class static block ----- parent class static constant ------- subclass static block -------- subclass static constant ---------- parent class common code block ------------ parent class construction method ---------- subclass code block ------------ subclass Construction Method

Class loading sequence 1. Static code block/static member

Static code blocks/static members are first loaded.

Static code blocks/static members are loaded sequentially according to the code writing sequence.

For example, if the parent class has static code blocks or static members, call the parent class static code blocks/static members (the Object has the highest priority) and load the static code blocks/static members of the class again.

2. Common Code blocks/non-static members

In the same class, common code blocks/non-static members are loaded after static code blocks/static members.

The loading sequence of multiple common code blocks/non-static members in the same class is successively loaded according to the code writing sequence.

For example, if the parent class has common code blocks/non-static members, the normal code blocks/non-static members of the parent class are loaded, and the normal code blocks/non-static members of the child classes are loaded.

3. Constructor

In the same class, the constructor loads the response code block.

If multiple constructor methods exist in the same class, they are loaded according to the called method of the instance.

First load the constructor of the parent class, and then load the constructor of the Child class.

The loading sequence of common code blocks/non-static members of child classes is lower than that of the parent class constructor.


4. Static methods/common methods

It is loaded only when called.

Summary:

Load Static code blocks/static members of the parent class first (there are or are multiple static code blocks in the parent class, in the writing order) ------------- Load Static code blocks/static members of sub-classes (there are or multiple word classes, in the writing order) --------- common code blocks/non-static members of the parent class (there are or more of the parent class, in the writing order) ------------ constructor of the parent class --------------- normal code block of the subclass/non-static members (there are or more word classes, in the writing order) ----------- constructor of the subclass



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.