Hi,
Please find below the code:
public class Static {
static{
int x=5;
}
static int x,y;
public static void main(
String args[]){
x--;
myMethod();
System.out.println(x+y+ ++x);
}
public static void myMethod(){
y=x++ + ++x;
}
}
Output is 3.
Can anyone please explain me the flow and why the output is 3?
Actually according to me the static variables x and Y are initialized to 0.After that when the static block runs the value of x is modified to 5.
As per the flow goes the answer should be 15.
Thanks in advance.