Modifier Keyword order in Java

2019-01-16 16:05发布

Every time I write method in Java with more keywords than public void, every time I write it another way. Sometimes "static public void" sometimes "public static void" etc.

What is the best order (best practices) for these keywords?

[abstract/static] [final] [synchronized] [public/private/protected] [result_type] ???

8条回答
孤傲高冷的网名
2楼-- · 2019-01-16 16:43

In theory it does not matter if you say public static final or final static public, but if you follow the usual convention, other people will able to read your code more easily. Here is the preferred order:

[ public | protected | private ]

static

abstract

synchronized

[ transient | volatile ]

final

native

strictfp

[ int | long | String | class | enum | interface etc. ]

查看更多
戒情不戒烟
3楼-- · 2019-01-16 16:49

Like this:

public static final synchronized void calculate()

查看更多
登录 后发表回答