A problem with 98% Java programmers

Source: Internet
Author: User
Tags constructor execution variable
Program | programmer | Question A student once asked me a strange question he had seen at Sun's Forum, because this question has not been answered, and the knowledge involved in this problem is somewhat biased, so I assert that at least 98% of the Java programmers do not!

(The reason for this assertion is that the pros and the low hand programmers I met seem to have no energy to study the trivial details and they don't seem to need it in their work.) If you see this problem, again to look up the book and think, finally find the answer, before this should be counted, hehe, in addition, 100 Java programmers have 2 will this, not too much, now have 2000 people read this article, there should be at least 40 people will this problem, Do not look at a few of the following will be ignored by those who do not express the views of the spectators, my 98% is not exaggerated.

If you do not this problem, does not mean that your level is lower than me oh, because this problem only I wait Kong Yi a few to play, you see, as a pastime.

Problem:

public class Parent
{
public void Test ()
{}

Public Parent ()
{
Test ();
}

public static void Main (string[] args)
{
New Child ();
}
}

Class Child extends Parent
{
private int instancevalue = 20;
public void Test ()
{
SYSTEM.OUT.PRINTLN ("Instance value is:" + instancevalue);
}
}


Let's guess what the print results are? Why, then?

----------------------------------------------------------------

Answer: Here is an excerpt from the Java Employment Training tutorial that I wrote, please note that the steps (4) and step (5) are explained, after the constructor method of the parent class is called, then the explicit initialization of the member variable is done, and the private int in the code above Instancevalue = 20; definition should be considered as two parts: the first part is to define a variable, the second is to assign a value to a variable, and the variable is defined before the parent class constructor, and the variable assignment is placed after the parent class constructs the method. When the parent class's constructor executes, according to Polymorphism, it invokes the test () method defined in the subclass, but at this point the member variable in the subclass does not perform an explicit initialization operation, for private int instancevalue = 20; instancevalue Value is the default initialization value of 0, so the value printed in the test method is 0.

4.1.3 The instantiation process of the subclass object

For many Java veterans, the instantiation of the subclass object is not necessarily clear, and you may not need to fully understand the instantiation of the subclass object, but it is good to know.

The initialization of a member variable in an object is based on the following steps:

(1) Allocating the storage space of the member variable and initializing it by default, that is, after the object is produced with the new keyword, the member variable in the class is initialized with the corresponding relationship in Table 3.1 of chapter three to initialize the member variable in the object.

(2) The binding constructor method parameter is the parameter assignment to the form parameter variable in the construction method of the new person (the actual argument list).

(3) If there is this () call, calls the appropriate overloaded constructor method (the overloaded constructor method that is invoked and executes the processes from step 2), after the execution process of the invoked overloaded construction method ends, returns to the current constructor method, and the current constructor jumps directly to step (6) execution. (Anyway, to invoke the constructor of the parent class, if you call this, the constructor that this point will call the parent class's constructor, and I will not have to call the constructor of the parent class, and if this is not called, then my constructor must call the constructor of the parent class.

(4) If there is no this () call, explicit or implicit tracing of the constructor method of the parent class (all the way to the object class, object is the topmost parent of all Java classes,/* is explained in detail later in this chapter), and the parent class constructs the process to execute these processes on the parent class starting with step 2. After the execution process of the constructor method of the parent class ends, the current construction method continues to execute.

(5) The explicit initialization of a member variable, that is, the execution of a statement that assigns a member variable when it is defined, such as:

Public Student extends person
{
String School = "it315";//Explicit initialization
......
}


Assign "it315" to a school member variable.

(6) Executes the program code in the method body of the current constructed method, such as:

Public Student extends person
{
Public Student (String name,int age,string School)
{
Super (Name,age);
This.school = School;
}
}



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.