简体   繁体   English

final static 和 static final 的区别

[英]Difference between final static and static final

I found a code where it declared code like我找到了一个代码,它声明了这样的代码

private final static String API_RTN_SUCCESS = "0";
private final static String API_RTN_ERROR = "1";

public static final String SHARED_PREFERENCE_CONFIG = "shared_preference_config";
public static final String STARTUP_SETTING_KEY = "startup_setting";

What is the difference between them or are they same?它们之间有什么区别或相同? Or does it differ for private or public ?还是privatepublic不同?

No difference at all.完全没有区别。 According to 8.3.1 - Classes - Field Modifiers of the Java Language Specification ,根据8.3.1 - 类 - Java 语言规范的字段修饰符

If two or more (distinct) field modifiers appear in a field declaration, it is customary, though not required, that they appear in the order consistent with that shown above in the production for FieldModifier.如果两个或更多(不同的)字段修饰符出现在字段声明中,虽然不是必需的,但通常它们的出现顺序与上面在 FieldModifier 的产生式中显示的顺序一致。

For fields, the said production lists the modifiers in this order:对于字段,上述产生式按以下顺序列出修饰符:

@Annotation public protected private static final transient volatile @Annotation public protected private static final transient volatile

And for methods:对于方法:

@Annotation public protected private abstract static final synchronized native strictfp @Annotation public protected private abstract static final synchronized native strictfp

They are the same.他们是一样的。 The order of modifiers is not significant.修饰符的顺序并不重要。 And note that the same rule applies in all contexts where modifiers are used in Java.请注意,相同的规则适用于 Java 中使用修饰符的所有上下文。

However, most Java style guides recommend/mandate the same specific order for the modifiers.但是,大多数 Java 风格指南都建议/强制要求修饰符采用相同的特定顺序。 In this case, it is public static final .在这种情况下,它是public static final

private static final String API_RTN_ERROR= "1";
private final static String API_RTN_ERROR= "1";
static private final String API_RTN_ERROR= "1";
static final private String API_RTN_ERROR= "1";
final static private String API_RTN_ERROR= "1";
final private static String API_RTN_ERROR= "1";

even all above are same the position of first three is intercangeable.即使以上都相同,前三个的位置是可以互换的。

They are same,他们是一样的,

private final static String API_RTN_ERROR = "1";

private static final String API_RTN_ERROR= "1";

No difference logically and no technical impact.逻辑上没有区别,也没有技术影响。

only issue is that sonarqube will report Code Smell if you use in this order唯一的问题是,如果您按此顺序使用,sonarqube 会报告 Code Smell

private final static <Type> <variable_name> = <value>;

Hence recommendation is to use:因此建议使用:

private static final <Type> <variable_name> = <value>;

The Java Language Specification recommends listing modifiers in the following order: Java 语言规范建议按以下顺序列出修饰符:

  1. Annotations注释
  2. public民众
  3. protected受保护
  4. private私人的
  5. abstract抽象的
  6. static静态的
  7. final最后
  8. transient短暂的
  9. volatile易挥发的
  10. synchronized同步的
  11. native本国的
  12. strictfp严格的fp

Note following this convention has no technical impact, but will reduce the code's readability because most developers are used to the standard order.请注意,遵循此约定没有技术影响,但会降低代码的可读性,因为大多数开发人员都习惯于标准顺序。

What is the difference between them or are they same?它们之间有什么区别或相同?

If you are talking about changing order of static and final then yes they are same.如果您正在谈论更改 static 和 final 的顺序,那么是的,它们是相同的。

does it differ for private or public?私人或公共有什么不同?

No, you can use any order in private and public.不,您可以在私人和公共场合使用任何订单。 Just difference is private variables will not be accessible outside of class directly.唯一的区别是私有变量不能直接在类外访问。

This is just a convention or a practice people follow to keep coding style consistent.这只是人们为了保持编码风格一致而遵循的约定或惯例。 It improves the readability.它提高了可读性。 so preferred way of writing this is所以写这个的首选方式是

private static final <Type> <variable_name> = <value>;

it's the same, of course.当然是一样的。 it only depends on your habits and preference:-).这仅取决于您的习惯和偏好:-)。 I use public static final order for members and methods too我也对成员和方法使用 public static final order

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM