# Could someone please explain what is going on here and why the output is 5,4,5? [closed]

Solution for Could someone please explain what is going on here and why the output is 5,4,5? [closed]
is Given Below:

I am currently learning on scope rules but I am still stuck on how this example program works, so if someone could explain what is happening at each step when the main method is executed that would be great

Edit: also what does the `s.a` and the `s.c` mean in the last line of the code?

``````import comp102x.IO;

public class Scope1 {

private int c = 1;
private int a = 2;

public int one(int a, int b) {
c = a * 2;
a = 3 + b;
b = b + 1;

IO.outputln("One: " + a + "," + b + "," + c);
return b;
}

public int two(int a, int b) {
c = one(b, a);
this.a = a + 1;
IO.outputln("Two: " + a + "," + b + "," + c);
return a;
}

public static void main(String[ ] args) {
Scope1 s = new Scope1();
int a = 4;
int b = 5;

b = s.two(a, b);
IO.outputln("Main: " + s.a + "," + b + "," + s.c); }
}
``````

Here is what is going on. Check my comments. You need to understand the difference between instance fields and local variables.

``````public class Scope1 {
private int c = 1;
private int a = 2;

public static void main(String[] args) {

Scope1 s = new Scope1();
int a = 4;
int b = 5;

b = s.two(a, b); // returning 4 from two and assigning to local b
// prints instance values a and c and local b.
IO.outputln("Main: " + s.a + "," + b + "," + s.c);
}

public int one(int a, int b) {
// you are passing in 5 and 4 from method two
c = a * 2; // c = 10    instance
a = 3 + b; // a = 7     local
b = b + 1; // b = 5     local
// now you print them
IO.outputln("One: " + a + "," + b + "," + c);
return b;  // and return 5
}

public int two(int a, int b) {
//you are passing in 4 and 5 from main
c = one(b, a);  // call one reversing the values. This sets
//instance c to 5
this.a = a + 1; // Instance field a = 5 and local b is unchanged.
// so you print 4,5,5 (local local instance)
IO.outputln("Two: " + a + "," + b + "," + c);
return a;
}

}
``````