If you ignore the details of Java, I am afraid your code will be full of bugs. Next we will discuss the issue of class member initialization.
First, initialize the member variable before the constructor
Refer to the Code in tij to illustrate the problem !!
1. Create a class tag
Package mark. initial; <br/> public class tag {</P> <p>/** <br/> * constructor <br/> * @ Param maker <br /> */<br/> Public tag (INT maker) {<br/> system. out. println ("tag (" + maker + ")"); <br/>}< br/>
2. Create a new class card
Package mark. initial; <br/> public class card {</P> <p> tag tag1 = new tag (1 ); </P> <p>/** <br/> * constructor <br/> */<br/> Public card () {<br/> system. out. println ("card ()"); <br/> tag tag3 = new tag (33 ); <br/>}</P> <p> tag tag2 = new tag (2 ); </P> <p>/** <br/> * member method <br/> */<br/> Public void F () {<br/> system. out. println ("F ()"); <br/>}</P> <p> tag tag3 = new tag (3); <br/>}< br/>
3. Create a test class
Package mark. initial; <br/> public class testinitial {</P> <p> Public static void main (string [] ARGs) {<br/> card c = new card (); <br/> C. F (); <br/>}< br/>
Let's analyze the results. There are tag objects everywhere in card, which looks messy. In fact, this is intentional. We know that the member variables of the class will be initialized to the default value, for example, the reference Initialization is null, the int is 0 by default, and the float is 0.0 by default, if you do not specify the values of these member variables.
In the test class testinitial, a new card is added, so that the member variables tag1, tag2, and tag3 are initialized to null first, (Because we manually assign these member variables a new value, that is, the new object), and then point to the object in the heap. Finally, card calls its own constructor, so the result is as follows:
Tag (1) <br/> tag (2) <br/> tag (3) <br/> card () <br/> tag (33) <br/> F ()
Second, the member variables are initialized after the constructor.
Initialize the subclass member variable. In the parent Linux, The subclass user overrides the print () method of the parent class and changes the member variable name value in this method.
Package my. test; public class Linux {int size = 10; string name = "Linux";/*** constructor */Public Linux () {system. out. println ("I'm Linux OS! "); Print ();} public void print () {system. out. println ("parent Linux -- print ()") ;}} class user extends Linux {string name = "ubuntu"; @ overridepublic void print () {name = "ubuntu10.10 "; system. out. println ("subclass user -- print ()");}}
Test code:
Class testlinux {</P> <p> Public static void main (string [] ARGs) {<br/> User user = new user (); <br/> system. out. println (user. name); <br/>}< br/>}
Create a user instance object user in the main method, and the user will call its own constructor. However, we know that the subclass will first call the constructor of the parent class in its constructor, in this way, the user's parent Linux will execute its own constructor to call the print () method. Because the sub-class user overwrites the parent class method, it calls the print () method of the sub-class () method. Change the value of name to ubuntu10.10. After the constructor is executed, initialize the user's member variable named "ubuntu". The printed result is as follows:
I'm Linux OS! Subclass user -- print () Ubuntu
The third type is actually another instance of the second type.
The following is the singleton mode:
Package my. test; <br/> public class classloadtest {<br/> // declare the class member variable and create the object <br/> static final classloadtest test = new classloadtest (); </P> <p> Public static int A = 5; <br/> Public static int B = 8; </P> <p>/** <br/> * private constructor <br/> */<br/> private classloadtest () {<br/> A ++; <br/> B ++; <br/>}</P> <p>/** <br/> * obtain instance objects of the class <br/> * @ return classloadtest Instance Object <br/> */<br/> Public static classloadtest getinstance () {<br/> return test; <br/>}< br/>}
Now, read the above Code and check the test code:
Class test {<br/> Public static void main (string [] ARGs) {<br/> system. out. println ("A =" + classloadtest. getinstance (). a); <br/> system. out. println ("B =" + classloadtest. getinstance (). b); <br/>}< br/>}
Before publishing the answer, most people will confidently say that the result is as follows:
A = 6 <br/> B = 9
You are so impulsive !!! Wrong answer. Think about it !!
The following code creates the classloadtest object test:
// Declare the class member variables and create the object <br/> static final classloadtest test = new classloadtest ();
In the test class, the constructor is called to obtain the instance of the class, that is, to create an object. In fact, this occurs at the following time:
<1> initialize test to null, a = 0, B = 0. The value here is the default value, not the values 5 and 8 you manually assigned.
<2> test = new classloadtest (), the constructor is called to make a = 1, B = 1
<3> for sequential code execution, static variables A = 5, B = 8, the original A = 1, B = 1 will be overwritten.
OK. The execution result should be:
A = 5 <br/> B = 8
If you change static final classloadtest test = new classloadtest (); in the Code, the results will be different. Change the classloadtest class as follows:
Package my. test; <br/> public class classloadtest {</P> <p> Public static int A = 5; <br/> Public static int B = 8; </P> <p> // declare a class member variable and create this object <br/> static final classloadtest test = new classloadtest (); </P> <p>/** <br/> * private constructor <br/> */<br/> private classloadtest () {<br/> A ++; <br/> B ++; <br/>}</P> <p>/** <br/> * obtain instance objects of the class <br/> * @ return classloadtest Instance Object <br/> */<br/> Public static classloadtest getinstance () {<br/> return test; <br/>}< br/>}
Use the above test method. The result is as follows:
A = 6 <br/> B = 9 <br/>
The analysis is as follows:
<1> initialize test to null, a = 0, B = 0. The value here is the default value, not the values 5 and 8 you manually assigned.
<2> A = 5, B = 8
<3> test = new classloadtest (), the constructor is called to make a = 6, B = 9
Summary:
Here, we should at least understand the following principles (for class member variables ):
<1> for Object-Oriented Programming, you also need to consider the order of declared variables.
<2> the Declaration and initialization in Java are not atomic operations, that is, they are not integrated. That is to say, after the Declaration, it has a default value, and the initialization value can be assigned manually.