• 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

Redeclaring a Variable in the Initialization Block

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

Is someone able to explain the below statement? I'm having trouble understanding the difference between the two examples and why the second one would compile?

Thanks

--


Redeclaring a Variable in the Initialization Block


This example looks similar to the previous one, but it does not compile because of the initialization block. The difference is that x is repeated in the initialization block after already being declared before the loop, resulting in the compiler stopping because of a duplicate variable declaration. We can fix this loop by changing the declaration of x and y as follows:


Note that this variation will now compile because the initialization block simply assigns a value to x and does not declare it.

 
Saloon Keeper
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically, the first clause of the "for" statement can either be an assignment statement or a declaration statement. So "int y =0, x=0" is an assignment statement, leading to the effect of the following:


Here the double-declaration is obvious. You can define and optionally assign multiple variables in an assignment statement, so "int y=0, x=0;" is equivalent to "int y =0; int x=0;" and NOT a declaration of y followed by an assignment without declaration of x.

As a rule, defining multiple variables in a single definition statement isn't encouraged these days.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . defining multiple variables in a single definition statement isn't encouraged . . .

I would have preferred that  to have been encouraged at all
 
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP

Is it Java Certification of some sort question? If yes, then I'd move (maybe copy) your question to another forum, we have a dedicated ones.
 
Suni Leeg
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies - i understand now.

Yes it is for OCA certification

I'm not sure how to move my post but i will use the dedicated forums in future - thanks for letting me know.

reply
    Bookmark Topic Watch Topic
  • New Topic