Given the following code:
public class Gurmesm {
private static int ad = 0;
private int hiae = 0;
public void striap(int di) {
A
int esh = 0;
ad += di;
esh += di;
hiae += di;
System.out.println("ad=" + ad + " esh=" + esh + " hiae=" + hiae);
}
public static void main(String[] args) {
B
Gurmesm g0 = new Gurmesm();
Gurmesm g1 = new Gurmesm();
g0.striap(1);
g1.striap(10);
g0 = g1;
g1 = new Gurmesm();
g0.striap(100);
g1.striap(1000);
C
}
}
hiae, ad, esh, g0, g1] are in scope at A ?Output:
hiae=1 ad=1 esh=1 hiae=11 ad=10 esh=10 hiae=111 ad=100 esh=110 hiae=1111 ad=1000 esh=1000
In scope at A : hiae, esh, ad
In scope at B : hiae, g0
In scope at C : hiae
Explanation (which you do not need to write out in your submitted solution):
hiae is a static variable, esh is an instance variable, and ad is a local variable.
At A , g0 and g1 out of scope because they are local to the main method.
At B , g1 is out of scope because it is not declared yet. esh is out of scope because it is an instance variable, but main is a static method. ad is out of scope because it is local to striap.
At C , g0 and g1 are out of scope because they are not declared yet. esh is out of scope because it is an instance variable, but main is a static method. ad is out of scope because it is local to striap.
Related puzzles: