Search code examples
javasynchronizedfinal

Java - 'synchronized' and 'final' method, declaration ordering


I have a question about the ordering of synchronized and final for a method. Both of the following are accepted by the compiler, but is there any actual difference between them?

public synchronized final void update() {
    // Do stuff
}

public final synchronized void update() {
    // Do stuff
}

Solution

  • From the JLS 8.4.3 :

    If two or more (distinct) method modifiers appear in a method declaration, it is customary, though not required, that they appear in the order consistent with that shown above in the production for MethodModifier.

    And the production MethodModifier order is specified as (see the JLS link I provided) :

    MethodModifiers:
        MethodModifier
        MethodModifiers MethodModifier
    
    MethodModifier: one of
        Annotation public protected private abstract
        static final synchronized native strictfp
    

    So JLS specifies that synchronized and final can appear in any order.

    Therfore the answer to your question is : There is no difference.