Page 1 of 1
Re: About Question enthuware.ocajp.i.v7.2.1245 :
Posted: Thu Apr 23, 2015 3:35 pm
by dmcinnis1
Hi,
I don't understand why moving line 3 before line 1 is a scope issue. Isn't the variable i in each for loop local only to the loop? Why would line 3 int i = 20 interfere with that?
Thanks
Re: About Question enthuware.ocajp.i.v7.2.1245 :
Posted: Thu Apr 23, 2015 7:15 pm
by admin
The problem is that a local variable can only be reference by its simple name. (Unlike, for example, an instance variable, which can also be referenced using "this" i.e. this.i or a class variable which can also be referenced using its class name such as MyClass.i ). If you move line 3 before 1, within the for loop, there will be two i's available. However, there will be no way to reference the outer local variable i (because it can only be referenced by its simple name i.e. i). This is called "obscuring" and is not permitted by Java.
You may want to go through section 6.4 of JLS to read more about it.
Re: About Question enthuware.ocajp.i.v7.2.1245 :
Posted: Fri Apr 24, 2015 11:47 am
by dmcinnis1
Got it. Thanks