This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
damien lepage
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
damien lepage
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marc. You're right, I missed this shadowing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic