I got this situation i cant understand about shadowing. For example the following code
class Foo {
int a = 5;
void goFoo(int a) {
// No problem naming parameter as same as instance variable
for (int a = 0; a < 5; a++) { }
//Now the compiler complains about the variable a on the for loop
// i thought that the loop block had its own scope so i could shadow
// the parameter, why the compiler didnt throw an error when i named
// the parameter same as the instance variable?
}
}
Thank you por you patience.
You can make a local variable shadow an instance/static variable - but you can't make one local variable (your loop counter) shadow another local variable or parameter (your parameter).
From the Java Language Specification, section 14.4.3:
Note the "field name" part - it's specifying that it has to be a field that is shadowed.
And from section 8.4.1:
(It goes on to talk about local classes and anonymous classes, but they're irrelevant in your case.)
But you don't declare the second "a" in that new scope, as your code shows. It's in the scope of the goFoo() block itself.
The problem isn't that the loop is shadowing the class field, the name is already used by the parameter.
Two options: One is to change the loop:
This uses the parameter as the index variable. Not clear why you would have a parameter, but all the same...
The other option is to rename the loop variable or the parameter to something else.
It is not shadowing, it is a conflict here. Both
a
are in the method scope. One cannot define two variables of same name in the same scope.The scope of the variable depends on the hierarchy of the block as well.
ie if u use like this
That is if a variable is declared in the outer block then you can not declare the same in the block which is inner. the other way you can do it.
it is similar to
so multiple declaration of
a
on the same scope, it is not acceptable.or simply it is similar to
Also See