• 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

Marcus Question Bank id:249

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
______________________________________________________________
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic