aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Objective 2.1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Objective 2.1" Watch "Objective 2.1" New topic
Author

Objective 2.1

frank neiderhiser
Greenhorn

Joined: Feb 15, 2012
Posts: 2


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
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
frank neiderhiser
Greenhorn

Joined: Feb 15, 2012
Posts: 2
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

Joined: Apr 02, 2012
Posts: 8
So is this the answer?
9 d 10 d
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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

ans. 91010d13


Tell the difficulties that i am difficult.
 
Don't get me started about those stupid light bulbs.
 
subject: Objective 2.1