• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

It is correct Why?

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int []a={1,2,3,4,5,6,7,8,9};
for(final int b:a)
System.out.println(b);


Variable b is declared final but it is correct why?
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every iteration of the for loop will create a variable b which is marked final. Since you are not changing the variable b,no problem.

try


[ September 18, 2008: Message edited by: Vijitha Kumara ]
 
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually after compilation, the enhanced for loop becomes a general for loop. So you loop becomes something like this

int []a={1,2,3,4,5,6,7,8,9};
for(int i = 0; i< a.length;i++)
{
final int b = a[i];
System.out.println(b);
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic