Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Mixing method modifiers and return type in a method declaration

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a mock exam question I thought that this was a good declaration of a method:

public void synchronized amethod3(){ }

I saw I was wrong when I review the answer. But the exact reason is not 100% clear to me. Doing some testing I think you can mix the order of all modifiers (access modiefiers, abstract, synchronized) but the return type of the method must be placed directly before the methods name. Is that correct?


Here is my test program I tried to compile:

class test {
public synchronized void amethod1(){ }
synchronized public void amethod2(){ }
public void synchronized amethod3(){ } // does not compile
void public amethod4(){} // does not compile
void public abstract amethod5(); // does not compile
}
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're right. see JLS
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic