• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marc. You're right, I missed this shadowing.
reply
    Bookmark Topic Watch Topic
  • New Topic