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;
}
}