File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objective 2.1

 
frank neiderhiser
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 1
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.

 
Dilip Mehra
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So is this the answer?
9 d 10 d
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dilip Mehra wrote:So is this the answer?
9 d 10 d

ans. 91010d13
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic