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
About Question enthuware.ocajp.i.v7.2.1245 :
Moderator: admin
-
- Posts: 16
- Joined: Wed Feb 25, 2015 8:52 pm
- Contact:
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1245 :
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.
You may want to go through section 6.4 of JLS to read more about it.
-
- Posts: 16
- Joined: Wed Feb 25, 2015 8:52 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1245 :
Got it. Thanks
Who is online
Users browsing this forum: No registered users and 2 guests