Java member variables static variable code block static code fast load order

Source: Internet
Author: User


Preface

Base Class A

Class B inheritance implements Class A

1 The class is loaded first when you start an instance of new B . (Classes are loaded by the Java class loader only when they are created with the New call) 2 A , 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, the order in which they appear in the installation code is initialized)
4 , after loading subclass B , Complete static action

1 B , first instantiate the parent class A
2 , instantiating the parent class A , first member instantiation (non-static code)
3 a
4 B
5 , subclass B Construction method


Initialize the static code of the parent class first ---> initializing the static code of a subclass - initializes the non-static code of the parent class ---> initializes the parent class constructor ---> Initialize subclass non-static code ---> Initialize subclass Constructors

Test code:

Abstract class Base
{
public int Age=getnumber (100);
static{
System.out.println ("Base static block");
}

{
System.out.println ("Base nonstatic block");
}
static int Sage=getnumber (50);
Base () {
System.out.println (age);
System.out.println ("base start");
Draw ();//Will call the subclass override method, here is 0!
System.out.println ("base End");
}
static int getnumber (int base) {
SYSTEM.OUT.PRINTLN ("Base.getnumber int" +base);
return base;
}
public void Draw () {
System.out.println ("Base.draw");
}
}

public class Initializeorder extends base{
public int Age=getnumber (1001);
private int _radius=getnumber (10);
static int Sage=getnumber (250);
static{
System.out.println ("Subclass static Block");
}
{
System.out.println ("Subclass nonstatic block");
}
Initializeorder (int radius) {
_radius=radius;
System.out.println (age);
Draw ();//here is 1000.
System.out.println ("Initializeorder initialized");
}
public void Draw () {
System.out.println ("Initializeorder.draw" +_radius);
}
public static void Main (string[] args) {//TODO auto-generated method stub
New Initializeorder (1000);

}

}

The output is:

Base static block
Base.getnumber Int50
Base.getnumber int250
Subclass Static Block
Base.getnumber int100
Base nonstatic block
100
Base Start
Initializeorder.draw 0
Base End
Base.getnumber int1001
Base.getnumber int10
Subclass Nonstatic Block
1001
Initializeorder.draw 1000
Initializeorder initialized



This article is from the "Android IOS" blog, so be sure to keep this source http://lovely1133.blog.51cto.com/1612511/1694264

Java member variables static variable code block static code fast 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.