Initialization order for class properties and object properties in Java classes

Source: Internet
Author: User
Tags constructor variables variable
Object Border town Madman





the initialization order of class attributes (static variables) and object properties (not static variables) in Java classes can be tested with the following program:





/**


* @ (#) Test.java


* @author Fancy


*/





public class Test {





static String a = "string-a";


static String B;





String c = "stirng-c";


String D;





Static {


printstatic ("before Static");


B = "String-b";


printstatic ("after Static");


}





public static void Printstatic (String title) {


System.out.println ("---------" + title + "---------");


System.out.println ("a = \" "+ A +" \ ");


System.out.println ("b = \" "+ B +" \ ");


}





public Test () {


print ("before constructor");


d = "string-d";


Print ("after constructor");


}





public void print (String title) {


System.out.println ("---------" + title + "---------");


System.out.println ("a = \" "+ A +" \ ");


System.out.println ("b = \" "+ B +" \ ");


System.out.println ("c = \" "+ C +" \ ");


System.out.println ("d = \" "+ D +" "");


}





public static void Main (string[] args) {


new Test ();


}





}





First, I'll comment out the main () method and run the results as follows:





---------before static---------


a = "string-a"


B = "null"


---------after static---------


a = "string-a"


B = "String-b"


Java.lang.NoSuchMethodError:main


Exception in thread "main"


Process completed.





Then, uncomment the main () method and run the results as follows:





---------before static---------


a = "string-a"


B = "null"


---------after static---------


a = "string-a"


B = "String-b"


---------before constructor---------


a = "string-a"


B = "String-b"


c = "Stirng-c"


d = "null"


---------after constructor---------


a = "string-a"


B = "String-b"


c = "Stirng-c"


d = "string-d"





This shows that Java class properties and object properties are initialized in the following order:





① the initialization of a class attribute (static variable) when defined, as in the previous example, static String a = "string-a";

The initialization code in the
②static block, such as B = "String-b" In the previous example static {};


③ the initialization of an object property (non-static variable) when defined, as in the example String c = "stirng-c";


④ the initialization code in the constructor method (function), such as D = "string-d" in the constructor method of the previous example;





The order of ② and ③ is not determined in this process because non-static variables cannot be printed in a static block. It is also for this reason that knowing their order does not help us write the program. Because class members (static members) are initialized when they are first used to a class, and object members (Non-static members) need to generate a class instance (that is, an object) to initialize, so I put ③ in the back of the ②. Once again, it is not clear from the code what the first of them is, and I am afraid it is necessary to study the execution process of the JVM (Java virtual machine).








The following is a discussion of some replies:


me to the Madman Tutorial "Object Members (Non-static members) need to generate a class instance (that is, an object) to initialize" some views! For example, you define the two object member variable string c = "Stirng-c" in the routine;


String D; I think that when a program executes this test class, the member variables (excluding constructors) and class member variables are loaded into memory. This allows the object member variable to be invoked by the constructor. But the exact sequence of static and non-static variables that can be read in the code is not very useful for writing code!


--Piggy Nunu


Static


{


printstatic ("before Static");


B = "String-b";


printstatic ("after Static");


} is loaded for the first time, and is performed only once.


--caterpillar








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.