Hi,
in one of the mock exam ... i face the question as follown
What will happen when you attempt to compile and run the following 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;
}
}
the o/p is 3 ...How ? so i put println statement in various place as follow
public class Static
{
static
{
int x = 5;
System.out.println("x= "+x);
}
static int x,y;
public static void main(String args[])
{
System.out.println("x= "+x);
x--;
System.out.println("x= "+x);
myMethod();
System.out.println("x= "+x);
System.out.println(x + y + ++x);
System.out.println("x= "+x);
System.out.println("y= "+y);
}
public static void myMethod()
{
System.out.println("x= "+x);
System.out.println("y= "+y);
y = x++ + ++x;
System.out.println("x= "+x);
System.out.println("y= "+y);
}
}
and its o/p is as follow -
D:\java_prac>
java Static
x= 5
x= 0
x= -1
x= -1
y= 0
x= 1
y= 0
x= 1
3
x= 2
y= 0
but my question is why variable y=0 when we done y = x++ + ++x;
pls tell me
regards