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

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends,



The aobve program prints as
output 4 3 2 1...although z being final..

Can anyone please give their suggestions on the above problem.Thank you in advance.

Regards,
Hardik.S.Raja
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
z is being redeclared each time in the loop, so it's ok as long as you're assigning a value only once. It would not work if you had : System.out.println(z++);
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

At each iteration a new var is declared and initialized with value of i, so
no problem;





Thanks,
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Referring to the original question: just think of the final as meaning that z has the same value (in the case of a primitive variable), or refers to the same object (in the case of a reference variable), throughout the current iteration. In the next iteration z has a different value.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry,


Referring to the original question: just think of the final as meaning that z has the same value (in the case of a primitive variable), or refers to the same object (in the case of a reference variable), throughout the current iteration. In the next iteration z has a different value.



Thats perfect about the highlighted part * the current iteration * !!
 
Ranch Hand
Posts: 358
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:
Referring to the original question: just think of the final as meaning that z has the same value (in the case of a primitive variable), or refers to the same object (in the case of a reference variable), throughout the current iteration. In the next iteration z has a different value.



Barry, that means, there is nothing like "redeclaration" or "new declaration", am I right? Please clarify!
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Faisal,


Barry, that means, there is nothing like "redeclaration" or "new declaration", am I right? Please clarify!



Even if its case, its applicable for Next Iteration only. Thats what Barry meant to say.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the program will not compile in first place as ia is not a boolean type. The middle statement in the for loop syntax should be boolean.

for(final int z=0; ia; )

here ia should evaluate to a boolean type otherwise program will not compile.
This would had compiled in a language such as c because there any non zero value would had been evaluated as true. but here it is not valid...
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kumar prashant:
[QB]I think the program will not compile in first place as ia is not a boolean type. The middle statement in the for loop syntax should be boolean.



kumar,

This thread is about "enhanced for loops" (aka for-each loops), a new feature in java 1.5.

If you are studying for the SCJP 1.4 certification, you'd better ignore this thread.
 
reply
    Bookmark Topic Watch Topic
  • New Topic