• 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 a for loop

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for(final int i:a){} //where a is an integer array.

This is allowed,while the following is not. why?

for( final int i =0;i<10;i++){}
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in first statement, the "final int i" is in local scope of "{}"
it is in fact the actual value of a[index], and index is increased

please take a look here:
http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.14.2
and see, how enhanced for loop is expanded
[ May 09, 2007: Message edited by: John Stone ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,



At each iteration new i is created and assigned a value. So there is no problem. And it (i) must be declared inside that for block and can't be used outside of the loop obviously.

While in this case :


Declaration/Initialization of i is done once, and in the iteration expression part we are trying to increment that i, hence error.



On Line #1 compiler complains:

The final local variable i may already have been assigned


I think compiler is not sure to anything directly.
But in the following case it speaks loudly:



Line #1 compiler complains:
The final local variable i cannot be assigned. It must be blank and not using a compound assignment


I am not so sure about this, but I get the working of both in that way.

If I miss something, please help me to get it clear.


Thanks,
[ May 09, 2007: Message edited by: Chandra Bhatt ]
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




On Line #1 compiler complains:

The final local variable i may already have been assigned


I think compiler is not sure to anything directly.


Not being very critical, but in which compiler version are you getting these errors? I get a different error on jdk for linux.
Anyways, the reason for the "may" in the error is that, since it is a loop and for one iteration, it will be valid assigning a value to a non-initialized final variable, for other iteration it will be invalid.




But in the following case it speaks loudly:


Line #1 compiler complains:
The final local variable i cannot be assigned. It must be blank and not using a compound assignment


Yeah, here since i is a initialized final variable, it is illegal to assign a value to it in any iteration.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Not being very critical, but in which compiler version are you getting these errors? I get a different error on jdk for linux.



It is javac 1.5.0_06

And the error what I got was really strange! What compiler meant by that,
I didn't understand well. But I can say, it a overall factor of scoping.
Using final variable (declare and use in that block only) inside the enhanced for loop is ok.


BTW, what does your compiler says, does it says it with surety or
in terms of "MAY BE"



Thanks,
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But I can say, it a overall factor of scoping.
Using final variable (declare and use in that block only) inside the enhanced for loop is ok.


The code in enhanced for loop converts to:


No statement tries to alter the value of the final var i after the initialization. This is the reason why it is fine.

java -version gives the following on my box:

java version "1.5.0_08"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_08-b03)
Java HotSpot(TM) 64-Bit Server VM (build 1.5.0_08-b03, mixed mode)

The compilation error is:

variable i might be assigned in loop

So, the "may be" is still there but in a different flavour "might be"
Jokes apart, i think the reason for "may be" is clear?
As the statement is an error not for the first iteration but for the iterations there on.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


int[] arr;
// Array initialization goes here
for(int count=0; count < arr.length; i++) {
final int i = arr[count];
}


No statement tries to alter the value of the final var i after the initialization. This is the reason why it is fine.



Does this compile?


This is working fine.

Regards,

Abdul Mohsin



Hmmm, It is not compiling in my BOX.

can not resolve "i"


Thanks,
[ May 09, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

int[] arr;
// Array initialization goes here
for(int count=0; count < arr.length; i++) {
final int i = arr[count];
}



This is working fine.

Regards,

Abdul Mohsin
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
the following code is not working for me:

quote:
--------------------------------------------------------------------------------
int[] arr;
// Array initialization goes here
for(int count=0; count < arr.length; i++) {
final int i = arr[count];
}
--------------------------------------------------------------------------------
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should be count++ and not i++.
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The code in enhanced for loop converts to:



No statement tries to alter the value of the final var i after the initialization. This is the reason why it is fine.



Also can I say, the final variable i goes out of scope in between the iterations, therefore everytime the control enters the block it is like creating a new variable i. correct?
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys sorry about the typo. The code indeed is:



Also can I say, the final variable i goes out of scope in between the iterations, therefore everytime the control enters the block it is like creating a new variable i. correct?


Yeah thats absolutely bang on
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
the folowing code is also not working:

int[] arr;// Array initialization goes herefor(int count=0; count < arr.length; count++) { final int i = arr[count];}

When i am trying to compile it is showing the error written below:
variable arr might not have been intialised

As far as my understanding goes the problem is becoz of teh local variable not being intialised i.e. arr not being intialised.

please clarify

thanks
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it should be

Reason being that arr got to be initialized before its used in arr.length.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


// Array initialization goes here



This comment was not to be ignored
 
reply
    Bookmark Topic Watch Topic
  • New Topic