Jump to content


Photo

Scope of variables


  • Please log in to reply
4 replies to this topic

#1 DreamingCode

DreamingCode

    Newbie

  • New Members
  • Pip
  • 3 posts

Posted 17 March 2014 - 01:00 AM

 
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?
 


#2 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,236 posts
  • LocationWA

Posted 17 March 2014 - 02:21 AM

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.

#3 DreamingCode

DreamingCode

    Newbie

  • New Members
  • Pip
  • 3 posts

Posted 17 March 2014 - 06:13 AM

wait so i dont get it, is it supposed to be x:15 y:25 z:10?



#4 DreamingCode

DreamingCode

    Newbie

  • New Members
  • Pip
  • 3 posts

Posted 17 March 2014 - 06:20 AM

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?



#5 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,236 posts
  • LocationWA

Posted 17 March 2014 - 04:15 PM

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 z
Variables 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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com