• 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
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

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: 25777
184
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: 76055
362
  • 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
 
Sheriff
Posts: 8324
594
Mac OS X VI Editor BSD Linux
  • 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.

 
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic