• 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

Objective 2.1

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


Can you please tell me why 8 isn't in the print out? this is from page 415/416 .
Also, what is the difference between x++ and static{x++};
K & B Book

Thanks!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

frank neiderhiser wrote:
Also, what is the difference between x++ and static{x++};



The "static { }" part of the code, in your example, is a static initializer. That code is run when the class is loaded, and ran with static variable initialization and other static initializers, in the order that they are encountered in source code. So ....

frank neiderhiser wrote:



Ignoring the main() method for the moment, when the class is loaded, a static variable "x" is allocated, and assigned the value of 7; then that variable is incremented in the static initializer. And then, the main() method is called.

Henry
 
frank neiderhiser
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry, that explains it all to me. So it is already 8 by the time it gets to the for loop, then it becomes 9.

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So is this the answer?
9 d 10 d
 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dilip Mehra wrote:So is this the answer?
9 d 10 d


ans. 91010d13
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic