Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Marcus Question Bank id:249  RSS feed

 
O Joseph
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question Bank id:249:
//snippet
class Slave implements Runnable{
int iPrice =100;
Master master;
Slave(Master m){
master=m;
}
synchronized public void setPrice(int iM){
iPrice=iM;
}
synchronized public void run(){
master.bContinue=true;
while(true){
System.out.println(iPrice);
}
}
}//end of code
i just saw this on marcuss site and thought a compiler error should have been generated 'cos run method was declared synchronized but instead the entire code was said to compile and output without error. ISNT THIS A CONTRADICTION?
______________________________________________________________
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any implemented method can be defined synchronized including run. Only non-implemented methods in an interface can not be defined synchronized.
 
O Joseph
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does this apply to other modifiers or just synchronized?
thanks
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not think this a general rule for all modifiers. For example you can not mix abstract and final...
[ September 20, 2003: Message edited by: Barkat Mardhani ]
 
Steve Lovelace
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe its like this: abstract methods, whether declared in a class or an interface, cannot be declared private, static, final, native, strictfp, or synchronized.
 
O Joseph
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Lovelace:
I believe its like this: abstract methods, whether declared in a class or an interface, cannot be declared private, static, final, native, strictfp, or synchronized...

is it correct if i complete your statement thus:...but when implemented in a class(or interface),can be declared private, static, final, native, strictfp, or synchronized(e.g. as in the example above).
Is this correct?
 
Steve Lovelace
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right (well, no implemented methods in interfaces). When a method is implemented it is not abstract, so the strictures no longer apply. The idea is that an abstract method only specifies access level, return type and signature. Whatever else is going on with the method is implementation detail.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!