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