I have the following code snippet taken from a mock exam for a certified Java programmer:
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;
}
}
The test asks you for the result of this line:
System.out.println(x + y + ++x);
The answer is 3, but I do not quite understand why this is 3. I can come up with this answer if I completely ignore it:
static
{
int x = 5;
}
My question is: what is the meaning of the above code snippet? Why does this not change the value of the variable "x"?
source
share