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