-1

I am super new to programming and I'm studying for a test and don't understand the output of a piece of code. I was hoping you could help me understand why the 4 is being printed at the end?

I threw the code in the Java visualizer site and I understand everything except why the line... System.out.println(myScope.z); would print the number 4?

Any help is greatly appreciated!

public class ScopeTest {
   int z;

public static void main(String[] args){

  ScopeTest myScope = new ScopeTest();
  int z = 6;
  System.out.println(z);
  myScope.doStuff();
  System.out.println(z);
  System.out.println(myScope.z);
}

void doStuff() {
  int z = 5;
  doStuff2();
  System.out.println(z);
}

void doStuff2() {
  z=4;
}
}
NoobCoderChick
  • 617
  • 3
  • 21
  • 40

5 Answers5

2
  1. ScopeTest myScope = new ScopeTest(); initializes myScope.z to 0 (default value)
  2. myScope.doStuff(); sets myScope.z to 5 and calls doStuff2()
  3. doStuff2() sets myScope.z to 4.

The final result being 4. Key point is that the local z variables declared in the main and doStuff() methods have no bearing whatsoever on the z member variable declared in ScopeTest.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
1

Stepping through code one line at a time is a good exercise to figure out what it is doing. You should do this with paper and pencil as well as with a debugger. So let's step through your code one line at a time in the order of execution. After each line of code, I'll explain what it does.

ScopeTest myScope = new ScopeTest();

Create a local reference variable named myScope and initialize it to an object.

int z = 6;

Create a local int variable named z and initialize it to 6.

System.out.println(z);

Print the value of the local variable z.

myScope.doStuff();

Call doStuff() on the myScope object.

int z = 5;

Create a local variable z in doStuff() and initialize it to 5.

doStuff2();

Call doStuff2()

z=4;

Set the member field named z to the value 4. And then return control back to doStuff().

System.out.println(z);

Print out the value of the local variable z. And then return control back to main().

System.out.println(z);

Print out the value of the local variable z. (Remember we are back in main() now.

System.out.println(myScope.z);

Finally, print out the member field z. (Remember its value was set back in doStuff2(). This value is remembered because this z is a member variable.)

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
1

This is the Flow:

myScope.doStuff() --> 1. z=5 2. doStuff2() --> z=4

Tal.Bary
  • 450
  • 2
  • 16
0

You are calling dostuff() in main() method, there you have initialized z=5 which is its local variable z and not class variable z. so then you are calling dostuff2() in which you are actually modifying class variable z=4. That is why it is printing z=4 in last syestem.out.println(z). Hope this will help you understanding output.

Programmer
  • 878
  • 2
  • 11
  • 24
0

because In your code z declared in the first line, is instance member (this context).

so when ever doStuff2() is called this.z is set to 4.

//effectively void doStuff2() { this.z=4; }

the variables int z declared inside the methods main and doStuff() are in local context.

and here, it only matters when the value of z in this context is set finally (which is inside doStuff2()). Thus,

System.out.println(z); >> prints value of local variable z declared in main System.out.println(myScope.z); >> prints the value of instance (of ScopeTest which is myscope here) member z

Shailesh Aswal
  • 6,632
  • 1
  • 20
  • 27