Java initialization trap

Source: Internet
Author: User

To illustrate this problem, let's give you a question:
See the Code:

Public abstract class A {</P> <p> Public A () {<br/> initmethod (); <br/>}</P> <p> public abstract void initmethod (); <br/>}

Public Class B extends a {</P> <p> static string staticstr = "static1"; </P> <p> private string teststr = "test1 "; </P> <p>/* <br/> * (non-javadoc) <br/> * @ see a # initmethod () <br/> */<br/> Public void initmethod () {<br/> // todo auto-generated method stub <br/> teststr = "Test2 "; <br/> staticstr = "static2 "; <br/>}</P> <p>/** <br/> * @ return the teststr <br/> */<br/> Public String getteststr () {<br/> return teststr; <br/>}</P> <p>/** <br/> * @ Param teststr <br/> * The teststr to set <br/> */<br /> Public void setteststr (string teststr) {<br/> This. teststr = teststr; <br/>}</P> <p>/** <br/> * create data: 2009-12-29 </br> <br/> * @ author xiangjie <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> B = new B (); <br/> system. out. println ("the variable teststr is:" + B. teststr); <br/> system. out. println ("the variable staticstr is:" + B. staticstr); <br/>}</P> <p>}

What are the output results?

Of course, this code is strange, but now it is just to illustrate this problem and you can write it casually. The problems encountered in your project may not be like this.

 

In order to explain this problem, we will not give the code first. Let's take a look at it step by step!

First, the static variable initialization problem occurs. static variable Initialization is performed during class loading, that is, when class initialization is not performed (Note: class loading before class initialization) staticstr = "static1" has been saved in the memory. In this case, you will find that staticstr = "static2" is changing the value already saved in the memory, the modification is made after initialization, so we should be clear about the printing results of static variables?

Next, let's analyze the class member variables.
This will involve the initial sequence of the class. B = new B (); it will be explained by the class initialization. When class initialization is performed, first, call the corresponding Constructor (Note: if not, the default constructor is B (). After Entering the constructor, it will first enter the constructor of the parent class. Obviously, our initmethod () is executed in this case, that is, the value of teststr = "Test2" at the moment, the execution of the parent class is complete, then enter the constructor of the subclass to continue the execution. after entering the subclass, initialize the class member variables first. This is very clear. Now teststr = "test1" is executed.

After the above analysis, the result should be clear, right?
Haha, the answer is published!
The variable teststr is: test1
The variable staticstr is: static2

 

 

More technical articles: http://www.eclipselight.org/rcp/193/

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.