my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in K&B SCJP 5: question 7 page 748 (chapter 9: Threads) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in K&B SCJP 5: question 7 page 748 (chapter 9: Threads)" Watch "Doubt in K&B SCJP 5: question 7 page 748 (chapter 9: Threads)" New topic
Author

Doubt in K&B SCJP 5: question 7 page 748 (chapter 9: Threads)

damien lepage
Greenhorn

Joined: Nov 25, 2007
Posts: 7
Hello,

In question 7 page 748, I think the answer C could be correct.

The question says only that a and b are two private variables, they're not necessarily primitives. Thanks to auto-boxing "a" could be an object of type Integer so we could synchronize on it.

Thanks

Damien


Damien Lepage
SCJP 93%, SCJD 94%
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Some of the questions in some exams not directly from Sun occasionally put harder questions in so that when you get to the Official Test, you pass the test, which is the true objective for everybody ... but when anyone says could be an object, I can be sure that I will not sync() on it. There are way too many issues in sync to let an ambiguity be the rest basis for one's design.
[ November 25, 2007: Message edited by: Nicholas Jordan ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by damien lepage:
...Thanks to auto-boxing "a" could be an object of type Integer so we could synchronize on it...

Welcome to JavaRanch!

That's good thinking, but if you try to verify this with some test code, I think you will find it does not work. Note that within the scope of the set method, 'a' is a local variable of type int -- it is not a reference to the instance variable 'a' of type Integer. Therefore, you will get a compilation error...

unexpected type
found : int
required: reference

So this would work if the block was synchronized on this.a instead of simply a. But that's not what's shown in option C.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
damien lepage
Greenhorn

Joined: Nov 25, 2007
Posts: 7
Thanks Marc. You're right, I missed this shadowing.
 
 
subject: Doubt in K&B SCJP 5: question 7 page 748 (chapter 9: Threads)
 
Similar Threads
How wait() and notify() works?
Question regarding Head First book chapter 7 mock question
Synchronized Block
Confusing in wait notify
notify() question from Nikos