This week's book giveaway is in the Open Source forum.
We're giving away four copies of RabbitMQ in Depth and have Gavin Roy on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static block and constructor problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of RabbitMQ in Depth this week in the Open Source forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static block and constructor problem" Watch "static block and constructor problem" New topic

static block and constructor problem

Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
source Certpal mock test

the answers given are
compile error

answer is 11
but why not 12
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
how 12?

scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later" someone unknown
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
when you instatiate cert you pass 10 and it calls a super constructor but super constructor does not save the increased value anywhere, and when x++ is executed in cert it has value 10 so after the statement it becomes 11.
and if you call super.x it will print 10 original value of x in super class.

SCJP 1.6 96%
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
When you are calling Grade(int x) constructor and incrementing x value i.e x++, means you are increasing local variables value, not the class's static variable's value(remember that concept of shadowing is applied here).

Sage of The Monstrous Toad of Mount Myoboku
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
yup got it rohan
thanks to neha and raju also
I agree. Here's the link:
subject: static block and constructor problem