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