Execution sequence Problems

Source: Internet
Author: User
Using System;
Using System. Collections. Generic;
Using System. text;

Namespace Staticdemo
{
Public   Class A
{
Public   Static   Int X;
Static A ()
{
X = B .y +   1 ;< BR >}

Public class B
{< br> Public static int Y = a.x + 1 ;
static B ()
{

}

}
Class Program
{
Static   Void Main ( String [] ARGs)
{
Console. writeline ( " X = {0}, y = {1} " , A.x, B .y );
Console. readkey ();
}
}
}

ProgramExecution sequence:

Output The a.x value, call static A () to assign a value to X, and use B .y to call static B () to obtain the B .y value. x = 2;

The B .y value is output. The constructor is empty and the initial value y = 1 is given;

Q: What is the output result after compilation?
The answer is: x = 2, y = 1
Explanation (from the Internet ):
1. In fact, I have seen questions about static constructor before, mainly understanding the sequence of execution of static constructor and constructor. This question is ingenious and upgraded to two types of static constructor and the sequence of initialization and execution of static members.
We all know that static member initialization and static constructor compilation are all part of static constructor, while static constructor is executed when the type is loaded. the headache of this question is, it is not obvious whether Class A is loaded before Class B or not. If Class A is loaded first, Class B will be loaded when class A's static constructor is executed.
+ 1 this expression has results before B type must be loaded, so y = 0 (x defaults to 0) + 1 = 1, x = 1 (Y is initialized) + 1 =
2. If Class B is initialized first, the result is the opposite. Finally, I think class A will be loaded first, and the result should be X = 2, y = 1. But in fact, I have no idea. -- Ivony (from: here)
2. The main method is the program entry, console. writeline ("x = {0}, y = {1}", a.x,
B .y
); The first access is a.x. Because X is a static member, the static constructor of a must have been executed before access to X, and B is accessed in the static constructor of. x. Similarly, in the static constructor of A, the static constructor of B must have been executed before the B .y value is obtained. Due to the completion of the static constructor of A, x = B .y
+
The execution process in the middle of 1 jumps to the static constructor of B, so a.x still defaults to 0, then, after the static constructor of B is executed, B .y can be obtained successfully in the static function of A. At this time, B .y = a.x
+
1 = 0 + 1 = 1, then a.x = 1 + 1 = 2
So the output is X = 2, y = 1
I also encountered this question during an interview. I gave a result and commented out "// not sure" after it was messed up. The interviewer asked me why after reading the answer, because I may be right, I admit that I am blind to the output results, but I answered his question with my own knowledge about static methods, I used the correct execution process to execute this process in my brain, but because I was very nervous at the time, I was not sure about the results I had made.

-- Xuefly (from here)

 

 

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.