开发者

what is the priority for static block in java?

开发者 https://www.devze.com 2023-03-28 18:18 出处:网络
public class Static { static { int x = 5; } static开发者_JAVA技巧 int x,y; public static void main(String args[])
public class Static
{
    static
    {
        int x = 5;
    }

    static开发者_JAVA技巧 int x,y;
    public static void main(String args[])
    {
        x--; myMethod();
        System.out.println(x + y + ++x);
    }

    public static void myMethod()
    {
        y = x++ + ++x;
    }
}

Could you please somebody help me here why it is displaying out put is 3?


  static
  {
        int x = 5;
  }

You redeclare x here, making it a locally scoped variable (not a class member). This assignment will have no affect whatsoever, regardless of when it is run.

Now, you asked about the static block and that's what I answered. If you are confused about why a value of 3 is outputted even assuming that assignment doesn't take place, then this becomes a question about the increment operators (x++ and ++x).

Full explanation

I like Paulo's explanation quite a bit but let's just see if we can simplify the code. To start, let's forget about making x and y a static field (making them local, initialized to the default for a static int: 0) and inline myMethod():

int x = 0, y = 0;
x--;
y = x++ + ++x;
System.out.println(x + y + ++x);

First we should eliminate complex expressions. We can do that by extracting each sub-expression into a temporary variable in the correct order (expressions are evaluated left-to-right):

int x = 0, y = 0;
x--;

int yOperand1 = x++;
int yOperand2 = ++x;
y = yOperand1 + yOperand2;

int resultOperand1 = x;
int resultOperand2 = y;
int resultOperand3 = ++x;
int result = resultOperand1 + resultOperand2 + resultOperand3;
System.out.println(result);

Now we can label the value of x, y and any temporary variables at each step:

int x = 0, y = 0;           //x: 0   y: 0
x--;                        //x: -1  y: 0

int yOperand1 = x++;        //x: 0   y: 0  yOperand1: -1
int yOperand2 = ++x;        //x: 1   y: 0  yOperand1: -1  yOperand2: 1
y = yOperand1 + yOperand2;  //x: 1   y: 0

int resultOperand1 = x;     //x: 1   y: 0  resultOperand1: 1
int resultOperand2 = y;     //x: 1   resultOperand1: 1  resultOperand2: 0
int resultOperand3 = ++x;   //x: 2   resultOperand1: 1  resultOperand2: 0  resultOperand3: 2
int result = resultOperand1 + resultOperand2 + resultOperand3; //result: 3
System.out.println(result);


Your static block has a local variable named x, it does not change the static variable with the same name.

In general, static { ... } blocks will be merged by the compiler with the initializers of static variables in the order they are in the code, and executed on class initialization. (In bytecode, this is a static <clinit> method.)

In your program the following occurs:

  • the static variables x and y are initialized with their default values (0).
  • the static block with its local variable is executed, this has no effect on the rest of the program.
  • the main method will execute.
    • it sets x = -1.
    • it invokes myMethod().
      • myMethod will twice increment x (from -1 to 0 to 1), and set y to 0 (the value before the first incrementing + the value after the second one).
    • it adds x (1), y (0) and the value after increasing x again (2), and prints the result (3).


Static.x starts with the value 0 since {int x = 5;} creates a new variable that is only visible within the static block. Since that x is never used it does nothing. If you removed it you should be able to trace through and see why your answer is what it is. :)

What you want is either to make the contents of the static block an assignment rather than a declaration ( ie static{ x = 5; }) or to assign x the value 5 at declaration static int x = 5;.

0

精彩评论

暂无评论...
验证码 换一张
取 消