Why is it that curly braces do not define a separate local scope in Java? I was expecting this to be a feature common to the main curly brace languages (C, C++, Java, C#).<
Run this and you'll get an error that the variable is already declared as int i=5
and one cannot redefine. So, the Parent never takes what its child got.
So, what if the Parent sacrifices and removes his declaration of int i=5
?
public class Ouch {
public static void main(String[] args) {
int i=5;
for(int i=0;i<5;i++);
for(int i=0;i<5;i++);
}
}
Now, the Parent sacrificed its declaration and both the children enjoy and the code runs successfully.
public class Ouch {
public static void main(String[] args) {
//int i=5;
for(int i=0;i<5;i++);
for(int i=0;i<5;i++);
}
}
The braces do scope the variable but anything inside a brace can also 'see' further up the brace. In all the cases you have, mine
is already defined as fido.
To put it more succinctly. The children are scoped to their parents as well but not vice versa.