Research on java code initialization process

Source: Internet
Author: User

Research on java code initialization process
I just saw an article on java code Initialization on ITeye. I tried to reason the result when I saw the code. Although it was a university knowledge, I did not expect to be able to do it right. (It seems that I have a good grasp of the basics in college, (* ^__ ^ *) Xi ......) However, the blog writing is not detailed enough. I would like to discuss in detail the specific initialization process of java code here. First of all, it should be clear that initialization is divided into two processes: class initialization and object initialization. Class initialization refers to the initialization process of class members when the class loader loads the class to the memory, that is, variables with static modification. For a loaded class, its class variables will be assigned a default value, even if you have assigned a value during definition. For example, the int type is 0, and the reference type is null. (The language is a bit pale and powerless. Let's just look at the example !) Public class Test {private static int number = 5; private static Object object = new Object ();} Let's analyze this instance. The Test class has two class member variables, after a class is loaded, it is necessary to initialize the class. Although the number and object are assigned a value during definition, they are first assigned a value of 0 and null, when will we assign our 5 and object objects? It is a static code block value after compilation. The actual initialization process is similar to the following code: public class Test {private static int number = 0; private static Object object Object = null; static {number = 5; object = new Object () ;}} object initialization refers to the initialization process when an Object is created. Its initialization process is similar to the class initialization process, there is only one more constructor process. Code: public class Test {private int number = 5; private Object object = new Object (); {// This is a dynamic code block, which is rarely used in reality, in fact, the number = 6;} public Test () {}} member variables are almost the same as directly defining member variables. I still feel that the code is clear, the actual initialization process is similar to the following code: public class Test {private int number = 0; private Object object Object = null; public Test () {number = 5; object = new Object (); number = 6 ;}} you are not mistaken. The dynamic code block is gone directly, and the value assignment process is moved to the constructor. Variable definitions and dynamic code blocks are moved to the constructor in the order of appearance. The above is the class initialization process and object initialization process. I don't know if I have made it clear. Let's take a few questions and test it. ^_^. (Take the code you just saw on ITeye as an example.) public class JvmTest {private static JvmTest = new JvmTest (); private static int count1; private static int count2 = 5; public JvmTest () {count1 ++; count2 ++;} public static JvmTest getInstance () {return JvmTest;} public static void main (String [] args) {System. out. println ("count1 =" + JvmTest. count1); System. out. println ("count2 =" + JvmTest. count2) ;}}don't know if you can use the above explanation to deduce what value to print. Here I will send the code of the actual initialization process: public class JvmTest {private static JvmTest = null; // This is the default value of private static int count1 = 0; // This is the default value of private static int count2 = 0; // This is the default value of static {JvmTest = new JvmTest (); count2 = 5;} public JvmTest () {count1 ++; // The pre-execution ++ value is 0 count2 ++; // The pre-execution ++ value is 0} public static JvmTest getInstance () {return JvmTest ;} public static void main (String [] args) {System. out. println ("count1 =" + JvmTest. count1); System. out. println ("count2 =" + JvmTest. count2 );}}

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.