Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Inquisition question

 
Jehaan Butt
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Which lines do not compile?
a. Line 1
b. Line 2
c. Line 3
d. Line 4

The answer given is 1 and 3. 3 is obvious - there is no semicolon. But the reason given for line 1 is that "Synchronized is wrong here". What does that mean? Why exactly is synchronized wrong in this statement?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The synchronized block cannot be placed outside a method. Furthermore you need to synchronize over an object. System.out.println() does not return an object.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... you need to synchronize over an object. System.out.println() does not return an object.

To clarify, the syntax needs to be:

Jim... ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic