Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
}
like image 857
Anju Maaka Avatar asked Mar 20 '26 14:03

Anju Maaka


1 Answers

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.

like image 130
Michał Krzywański Avatar answered Mar 22 '26 06:03

Michał Krzywański



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!