Borg
Science Advisor
Gold Member
- 2,296
- 5,033
This does not compile. The a and b defined in Scope A are visible throughout and cannot be redefined.voko said:Code:{ // scope A int a; int b; { // scope B - nested in A int a; { // scope C - nested in A nested in B int b; } } { // scope D - nested in A int b; } }
Not true. Static, class level variables can be accessed via 'this'. It just isn't correct to do so because the value is applicable to all of the classes that you have created and not just one particular instance. IDEs and compilers will warn you about it (Static member accessed via instance reference) but the code will compile and function properly.voko said:Now, even though I said that an outer scope's variables eclipsed by an inner scope's variables are inaccessible, there is a way to use the class scope variables by referencing them via this, but that can only be done in a non-static member function (because static members do not have this to begin with).
Think of it as saying "while I'm in this method, I don't care that there is a class level variable x. I want x to be something else". If you haven't made x to "be something else", x and this.x both refer to the class level variable. Once you decide to make it something else, only this.x will refer to the class level version (within the current scope).CAF123 said:I am not sure what you mean by 'using my one shot at reusing the class level variable name'.
superman is on the right side. You aren't setting superman, you're setting x. There is no local variable x that exists at that point so x and this.x both refer the same thing (the class level x). The compiler doesn't look ahead to see that you're going to create a local version of x on the next line.CAF123 said:I don't think I understand this paragraph fully. Does it not contradict the comments after the code:
So x = superman; sets the local variable superman to the class level variable x? x = superman; sets the class level variable, (as written in the comment after the code) but above you say this.x modifies it.Code:x = superman; // The incoming variable superman is being used to set the class level variable x
The brackets could represent anything that has brackets like an if statement, a for loop, or just a set of brackets. The brackets define a scope that is walled off from other areas. Inside the scope, the compiler knows what has been defined previously but can't look inside other bracketed areas.CAF123 said:Why would you put the String in between those brackets? I don't think I have grasped this part either.
Because it makes the code easier to understand? You're not really reusing the variables, just their names. Yeah, I know that doesn't make sense yet...CAF123 said:I appreciate your help, but I don't feel like I understood it as well as the other examples. One question that sprung to mind was why reuse variables?
Here's another example of scope within a method. I am going to ignore the whole x vs. this.x issue. For now, I'm just showing you a single method. I'm also using all new variable names because I don't want you thinking about anything previously discussed. Just try to understand what is going on with the variable u.
Let's say that we have a class level variable s that can be modified by other methods in your class and we need a method to perform different actions depending on the current value of s. In this method, I am not modifying the value of s. However, I am creating a variable t at the beginning and defining a variable u in the two sections of an if/else statement. As far as the compiler is concerned, u can be defined inside either of the brackets because it wasn't defined earlier (like t). Creating another u within the else statement isn't a problem because even though it can look back for previously defined variables, it can't peek inside the brackets. As far as the code in the else brackets is concerned, u has not been defined yet (it can't see inside of the if brackets while it's inside of the else brackets).
Code:
int s = 10; // Some class level variable
public int someRandomMethod(){
int t = 2;
if (s > 100){
int u = 2 / s;
if(u > 250){
return u; // Leave the method and return the value of u
}
// If u <= 250 the code will leave the if/else block and continue at the EXIT POINT
}
else{ // If s was greater than 100, it won't go in here
int u = (10 * t) - s;
if(u < 25){
return u;
}
// If u <= 25 the code will leave the if/else block and continue at the EXIT POINT
}
// EXIT POINT
// The compiler knows about s and t
// The compiler does not know about u at this point because it can't look up and into brackets
return 0;
}
Last edited: