Given the following code:
public class Prihi {
private static int e = 0;
public static void main(String[] args) {
Prihi p0 = new Prihi();
A
Prihi p1 = new Prihi();
B
p0.ceulod(1);
p1.ceulod(10);
p1 = p0;
p0.ceulod(100);
p0 = new Prihi();
p1.ceulod(1000);
}
private int sust = 0;
public void ceulod(int na) {
int huc = 0;
C
sust += na;
e += na;
huc += na;
System.out.println("sust=" + sust + " e=" + e + " huc=" + huc);
}
}
huc, sust, e, p0, p1] are in scope at A ?Output:
huc=1 sust=1 e=1 huc=10 sust=11 e=10 huc=101 sust=111 e=100 huc=1101 sust=1111 e=1000
In scope at A : sust, p0, p1
In scope at B : sust, p0, p1
In scope at C : sust, huc, e
Explanation (which you do not need to write out in your submitted solution):
sust is a static variable, huc is an instance variable, and e is a local variable.
At A , huc is out of scope because it is an instance variable, but main is a static method. e is out of scope because it is local to ceulod.
At B , huc is out of scope because it is an instance variable, but main is a static method. e is out of scope because it is local to ceulod.
At C , p0 and p1 out of scope because they are local to the main method.
Related puzzles: