public class ScopeExample {
static int x = 10;
public static void main(String[] args) {
int y = 20;
System.out.println("Before if statement - x: " + x + ", y: " + y);
if (x > y) {
int z = 30;
System.out.println("Inside if statement - z: " + z);
}
System.out.println("After if statement - x: " + x + ", y: " + y);
}
}
Explain the scope and lifetime of variables x
, y
, and z
in the above code.
Write your answer here.