• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

final variable in loop

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for(final int i : intarray){ } this is legal , but how is it possible for a fianl variable to change its value in each iteration??
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rajasekhar Devi Reddy:
for(final int i : intarray){ } this is legal , but how is it possible for a fianl variable to change its value in each iteration??



It's really weird isn't it? The final keyword is to prevent the developer from changing the variable -- which technically he/she is not.

Interestingly, this is not normally done with a regular for loop, because it is common to change the index declared in the initialization expression (of the for loop), in the re-initialization expression (of the for loop).

Henry
[ January 10, 2008: Message edited by: Henry Wong ]
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Henry, I have one doubt,
for(final int i : intarray){ }
In the above statement does the statement final int i execute with each iteration of the loop?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dean, I think you are right..
final is ok here because of the reason that the enhanced for actually is just an equivalent of a basic for statement which uses a different loop variable ( other than the one specified in the for loop).



Enhanced for loop is of the form:

for (VariableModifiers Type Identifier : Expression ) Statement



An example is :

for (final int i: intArray)
{
//statements herre
}

final is an example of VariableModifiers(optional).
int is an example of "Type"
i is referred to as "Identifier"
intArray is an example of Expression. It is of array type.


When the Expression is an array type,this is equivalent to a basic for statement of the form :

int[] a = intArray;

for (int k=0; k < a.length; k++)
{
final int i = a[k]; -------------(1)

//statements here

}

(1) implies that the variable i becomes a local variable in the for loop which can have final as the modifier.

a and k are compiler-generated identifiers that are distinct from any other identifiers that are in scope at the point where enhanced for loop occurs.


Please refer to 14.14.2 Pg 387 JLS 3rd edition

[ January 11, 2008: Message edited by: Raman Gopalan ]
[ January 11, 2008: Message edited by: Raman Gopalan ]
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Raman is right, Would like to elaborate his point :

As he was clear that i becomes a local variable, and as a basic concept of every programing language, as soon as first iteration finish, scope of i finish infact, there is no virutefor i : so in the next iteration we can define a new variable with same name and same attributes, so in this case final i is acceptable.
 
reply
    Bookmark Topic Watch Topic
  • New Topic