About Question enthuware.ocajp.i.v7.2.1245 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
dmcinnis1
Posts: 16
Joined: Wed Feb 25, 2015 8:52 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1245 :

Post 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

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1245 :

Post 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.

dmcinnis1
Posts: 16
Joined: Wed Feb 25, 2015 8:52 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1245 :

Post by dmcinnis1 »

Got it. Thanks

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests