什么是Java修饰符的合理顺序(抽象,最终,公共,静态等)? [英] What is a reasonable order of Java modifiers (abstract, final, public, static, etc.)?

查看:135
本文介绍了什么是Java修饰符的合理顺序(抽象,最终,公共,静态等)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java修饰符的合理顺序是什么?

What is a reasonable order of Java modifiers?


  • abstract

  • final

  • native

  • private

  • protected

  • public

  • static

  • strictfp

  • synchronized

  • transient

  • volatile

  • abstract
  • final
  • native
  • private
  • protected
  • public
  • static
  • strictfp
  • synchronized
  • transient
  • volatile

更新

我已将推荐的措辞改为合理的,以便冷静讨论是否推荐订单。

I have changed the wording from recommended to reasonable in order to calm down the discussions whether the order is recommended or not.

推荐答案

修饰符的习惯使用顺序在 Java语言规范(而不是Java虚拟机规范)例如对于课程修饰符你会发现以下定义(摘录):

The customary usage order of the modifiers is mentioned in the Java Language Specification (and not the Java Virtual Machine Specification) e.g. for class modifiers you will find the following definition (extract):


ClassModifiers:
    ClassModifier
    ClassModifiers ClassModifier

ClassModifier: one of
    Annotation public protected private
    abstract static final strictfp

[....]

如果两个或更多(不同的)类修饰符出现在类声明中,则它是虽然不是必需的,但它们按照与上面在ClassModifier的制作中所示顺序一致的顺序出现。 (段落底部的小文字!)

If two or more (distinct) class modifiers appear in a class declaration, then it is customary, though not required, that they appear in the order consistent with that shown above in the production for ClassModifier. (small text at the bottom of the paragraph!)

你会在指定使用修饰符的其他几个地方找到这句话,例如对于字段此处修饰符。

You will find this sentence at several other places where the usage of modifiers is specified, e.g. here for field modifiers.

更新 :我将指定/推荐替换为惯用,以使其成为可接受的答案。如果您阅读评论,请考虑到这一点;-)(感谢@EJP明确表示) - 然而推荐使用习惯订单。

Google还建议使用Java规范中提到的惯例订单。

Google also recommends using the customary order mentioned in the Java spec.

public / protected / private 
abstract 
static 
final 
transient 
volatile 
synchronized 
native 
strictfp

更新 :有一个新的 Java风格指南为OpenJDK社区中的项目制定了计划。它还建议修改订单还包括新的默认 Java 8的修饰符。

Update: There is a new "Java Style Guidelines" initiative in place for projects in the OpenJDK community. It also has a recommendation for a modifier order and also includes the new default modifier of Java 8.

public / private / protected
abstract
static
final
transient
volatile
**default**
synchronized
native
strictfp

这篇关于什么是Java修饰符的合理顺序(抽象,最终,公共,静态等)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆