DreamingCode Posted March 17, 2014 Share Posted March 17, 2014 public class FindInteger { int x = 10; int y = 20; int z = 30 ; int m1(int y, int z) { int x = 15; y = x + z; this.y = y + 10; IO.outputln("x: " + x + " y: " + y + " z: " + z); return y; } the output of this simple program i wrote gives x:15 y:25 z:10. My question is why is it giving me these values? Shouldnt it be giving me x:15 y:45 z:30? Quote Link to comment https://forums.phpfreaks.com/topic/287020-scope-of-variables/ Share on other sites More sharing options...
requinix Posted March 17, 2014 Share Posted March 17, 2014 Java and Javascript are two completely different things. Yes, there are class variables named x, y, and z, but the method you have defines it's own versions. It's called variable shadowing. Quote Link to comment https://forums.phpfreaks.com/topic/287020-scope-of-variables/#findComment-1472856 Share on other sites More sharing options...
DreamingCode Posted March 17, 2014 Author Share Posted March 17, 2014 wait so i dont get it, is it supposed to be x:15 y:25 z:10? Quote Link to comment https://forums.phpfreaks.com/topic/287020-scope-of-variables/#findComment-1472866 Share on other sites More sharing options...
DreamingCode Posted March 17, 2014 Author Share Posted March 17, 2014 public class FindInteger { int x = 10; int y = 20; int z = 30 ; int m1(int y, int z) { int x = 15; y = x + z; this.y = y + 10; IO.outputln("x: " + x + " y: " + y + " z: " + z); return y; } void m2( ) { int z=40; int y = m1 (x, x); if ( z < 50 ) { int x = 90; z = x + y; } x = x + y + z; IO.outputln("x: " + x + " y: " + y + " z: " + z); } public static void main(String[ ] args){ FindInteger q = new FindInteger( ); q.m2( ); IO.outputln("x: " + q.x + " y: " + q.y + " z: " + q.z); } } this is a full code of this particular class i wrote, however its giving me values which i dont agree with. It gives me x: 15 y:25 z: 10 x: 150 y:25 z: 115 x: 150 y:35 z: 30 i dont understand this.. why is z=10? also why is z=30 on the last line? Quote Link to comment https://forums.phpfreaks.com/topic/287020-scope-of-variables/#findComment-1472868 Share on other sites More sharing options...
requinix Posted March 17, 2014 Share Posted March 17, 2014 Bear in mind that the code is deliberately trying to confuse you. And doing it well - I'm having a hard time following it all in my head. It's a bitch to explain too. Disclaimer: this is the short explanation that tries to address how other languages (except PHP, ironically enough) manage scope too. Java has a hierarchy of scopes - places that variables can be found. At the top is class scope, below that is method scope, and below that there can be a number of block-level scopes, each of which could have more block-level scopes inside. - Block-level scope, such as inside an if or loop. The variable is destroyed when you leave that scope. if (z < 50) { int x = 90; // defines x but only within this if block- Method scope, such as in the method signature. int m1(int y, int z) { // defines y and zVariables can also be defined inside a method (and outside any loops or such). void m2() { int z = 40; // defines z but only within this method- Class scope, which is where FindInteger has definitions for x, y, and z. public class FindInteger { int x = 10; int y = 20; int z = 30;When you refer to a variable "x", Java looks for the variable starting at the current scope and works its way "up" the hierarchy: first in the current block (if any), then up each block (if any), then finally to the method and then to the class. Here is a fun diagram of where the variables are defined. FindInteger public class FindInteger { +------------+ | | int x = 10; | x | int y = 20; | y | int z = 30; | z | | | | m1 | | +--------+ | int m1 (int y, int z) { | | y, z | | int x = 15; | | x | | y = x + z; | | | | this.y = y + 10; | | | | IO.outputln(...); | | | | return y; | | | | } | +--------+ | | | | m2 | | +--------+ | void m2() { | | | | int z = 40; | | z | | int y = m1(x, x); | | y | | | | | | | | if | | if (z < 50) { | | +----+ | | int x = 90; | | | x | | | z = x + y; | | | | | | } | | +----+ | | | | | | x = x + y + z; | | | | IO.outputln(...); | | | | } | +--------+ | | | | main | | +--------+ | void main(String[] args) { | | args | | FindInteger q = ...; | | q | | q.m2(); | | | | IO.outputln(...); | | | | } | +--------+ | } +------------+ When you're looking for a variable, you start inside whatever block you're in at the time and work your way outwards until you find it. Here's a crazy explanation of the execution: FindInteger q = new FindInteger; // q.x=10 q.y=20 q.z=30 q.m2(); +--entering m2 scope-- // x->this.x=10 y->this.y=20 z->this.z=30 | void m2() { // x->this.x=10 y->this.y=20 z->this.z=30 | int z = 40; // x->this.x=10 y->this.y=20 z=40 | int y = m1(x, x); // x->this.x=10 y=? z=40 | | +--entering m1 scope-- // x->this.x=10 y->this.y=20 z->this.z=30 | | int m1(int y, int z) { // x->this.x=10 y=10 z=10 | | int x = 15; // x=15 y=10 z=10 | | y = x + z; // x=15 y=(15+10)=25 z=10 | | this.y = y + 10; // x=15 y=25 z=10 this.y=25+10=35* | | IO.outputln(...); // "x: 15 y: 25 z: 10" | | return y; // x=15 y=25 z=10 | | } | +--leaving m1 scope-- // x->this.x=10 y=25 z=40 | | if (z < 50) { // x->this.x=10 y=25 z=40 | | +--entering if scope-- // x->this.x=10 y->m2's y=25 z->m2's z=40 | | int x = 90; // x=90 y->m2's y=25 z->m2's z=40 | | z = x + y; // x=90 y->m2's y=25 z->m2's z=(90+25)=115* | | } | +--leaving if scope-- // x->this.x=10 y=25 z=115 | | x = x + y + z; // x->this.x=(10+25+115)=150* y=25 z=115 | IO.outputln(...); // "x: 150 y: 25 z: 115" | } +--leaving m2 scope-- // q.x=150 q.y=35 q.z=30 IO.outputln(...); // "x: 150 y: 35 z: 30" *s are where variables from outer scopes get modified, and where you're losing track of values. I suggest you try a smaller bit of code with fewer variables to keep track of. Play around with methods and class variables and ifs and loops and variable declarations and stuff, see what happens. That's the best part about programming: if you're not sure how something will go, try it and find out. Quote Link to comment https://forums.phpfreaks.com/topic/287020-scope-of-variables/#findComment-1472934 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.