Initialization order for Java classes and subclasses

Source: Internet
Author: User

This article is reproduced here to thank the original author.

Original post address: http://www.iteye.com/topic/208187

When you go to an interview, you often encounter such questions as: give you two classes of code, they are inherited relationships, each class only constructor method and some variables, the constructor may have a piece of code to the variable value of some kind of operation, in addition to the variable value output to the console code, Then let's judge the output. This is actually an examination of our understanding of the initialization order of classes under inheritance.

1, for static variables, static initialization blocks, variables, initialization blocks, constructors, their initialization order is (static variable, static initialization block) > (variable, initialization block) > constructor. We can also verify this by using the following test code:

public class Initialordertest {

	//static variable public
	static String Staticfield = "static variable";
	Variable public
	String field = "Variable";

	Static initialization block static
	{
		System.out.println (Staticfield);
		SYSTEM.OUT.PRINTLN ("Static initialization Block");
	}

	Initialize block
	{
		System.out.println (field);
		System.out.println ("initialization block");
	}

	Constructor public
	Initialordertest () {
		System.out.println ("constructor");
	}

	public static void Main (string[] args) {
		new initialordertest ();
	}
}

Program output:

static variables
Static initialization block
Variable
Initializing blocks
Construction device

This is in full conformity with what has been said above.

2, what about inheritance? We still use a piece of test code to get the final result:

Class Parent {
	//static variable public
	static String P_staticfield = "Parent class-static variable";
	Variable public
	String P_field = "Parent class-variable";

	Static initialization block static
	{
		System.out.println (P_staticfield);
		System.out.println ("Parent class-static initialization block");

	Initialize block
	{
		System.out.println (P_field);
		System.out.println ("Parent class--initialization block");

	Constructor public
	parent () {
		System.out.println ("Parent class--constructor");
	}

public class Subclass extends Parent {
	//static variable public
	static String S_staticfield = subclass-static variable;
	Variable public
	String S_field = "Subclass--variable";
	Static initialization block static
	{
		System.out.println (S_staticfield);
		System.out.println ("Subclass--static initialization block");
	Initialize block
	{
		System.out.println (S_field);
		System.out.println ("subclass--initialization block");

	Constructor public
	Subclass () {
		System.out.println (subclass--constructor);
	}

	Program entry public
	static void Main (string[] args) {
		new subclass ();
	}
}

Run the above code, the results immediately appear in front of our eyes:
Parent class--static variable parent class--static initialization block subclass--Static variable Quantum class--static initialization block parent class--Variable parent class--Initialize block parent class--constructor subclass--Variable Quantum class--initialization block subclass--constructor

Now, the results have been self-evident. You may notice that the initialization of subclasses is not done until the parent class is fully initialized, and that the initialization of static and static initialization blocks of subclasses is done before the variables, initialization blocks, and constructors of the parent class are initialized.

So what is the sequence between a static variable and a static initialization block, a variable, and an initialization block? Whether a static variable is always preceded by a static initialization block, the variable is always initialized before the initialization block. It actually depends on the order in which they appear in the class. We use static variables and static initialization blocks as examples to illustrate.

3, again, we're going to write a class to test:

public class Testorder {
	//static variable public
	static Testa a = new Testa ();
	
	Static initialization block static
	{
		System.out.println ("Static initialization Block");
	}
	
	Static variable public
	static TESTB B = new Testb ();

	public static void Main (string[] args) {
		new Testorder ();
	}
}

Class Testa {public
	testa () {
		System.out.println ("test--a");
	}

Class Testb {public
	testb () {
		System.out.println ("Test--b");
	}

Running the code above will result in the following:
Test--a Static initialization block Test--b


You can change the variable a, variable B and the position of the static initialization block before and after, you'll find that the output changes with the order in which they appear in the class, which means that the static variables and the static initialization blocks are initialized according to the order in which they are defined in the class. Similarly, variables and initialization blocks follow this rule. After the

understands the initialization order of classes under inheritance, how to determine the final output is solved.

Related Article

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.