[英]Private Final vs Final Private
Did a few searched on the inter-webs and could not find an easy answer for this.在互联网上搜索了一些,但找不到简单的答案。 My problem set is using Java within the Android Framework, but I believe this is just standard Java behavior as well.
我的问题集是在 Android 框架中使用 Java,但我相信这也只是标准的 Java 行为。 I understand the definitions of
final
and private
, both used for variable access and modifiers.我了解
final
和private
的定义,它们都用于变量访问和修饰符。 I was following some code and tutorials when the instructions asked me to initialize a variable as a final private
variable.当说明要求我将变量初始化为
final private
变量时,我正在学习一些代码和教程。 Not having seen this before, I instead made a private final
variable.以前没有见过这个,我改为创建了一个
private final
变量。 Later on, the code asked me to initialize the variable in a constructor, which obviously fails against a private final
variable as it is immutable.后来,代码要求我在构造函数中初始化该变量,这显然对
private final
变量失败,因为它是不可变的。 HOWEVER, it did not fail when I changed the variable to a final private
... which made me interested.但是,当我将变量更改为
final private
时它并没有失败......这让我感兴趣。 Does anyone have ideas why this is the case?有谁知道为什么会这样?
Thanks for the responses!感谢您的回复!
The Java Language Specification, section 8.3.1. Java 语言规范,第8.3.1节。 Field Modifiers , says:
字段修饰符,说:
FieldModifier: (one of) Annotation public protected private static final transient volatile
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 产生式中所示的顺序一致。
Which means that private final
is the preferred style, but it's exactly the same as final private
.这意味着
private final
是首选样式,但它与final private
完全相同。
Later on, the code asked me to initialize the variable in a constructor, which obviously fails against a private final variable as it is immutable.
后来,代码要求我在构造函数中初始化该变量,这显然对私有 final 变量失败,因为它是不可变的。
Members marked as final
can be initialized in constructor.标记为
final
成员可以在构造函数中初始化。 You can either create a prameterized constructor which accepts values from outside of class or simply initialize those members directly with declaration.您可以创建一个参数化构造函数,该构造函数接受来自类外部的值,也可以直接使用声明来初始化这些成员。 If you choose later and the data type is primitive, then mark those members as
static
since they are going to remain same for all instances of class.如果您稍后选择并且数据类型是原始数据,则将这些成员标记为
static
因为它们对于类的所有实例都将保持不变。
HOWEVER, it did not fail when I changed the variable to a final private... which made me interested
但是,当我将变量更改为最终私有时它并没有失败......这让我感兴趣
This won't be possible since private final
and final private
and are virtually the same thing and won't make any difference.这是不可能的,因为
private final
和final private
几乎是一回事,不会有任何区别。 However former is considered as good practice.然而,前者被认为是良好的做法。 You can follow this SO for more details regarding ordering.
您可以按照此SO了解有关订购的更多详细信息。
As an example, here are a couple cases of things that are ok and not ok with final
.举个例子,这里有一些可以使用
final
和不可以使用的情况。 There is no difference between private final
and final private
. private final
和final private
之间没有区别。 To add to what @Sagar said, you can initialize the variables inline, or in the constructor, but not both.添加@Sagar 所说的内容,您可以内联或在构造函数中初始化变量,但不能同时初始化。
public class TestClass {
private final String i1;
final private String i2;
private final String i3 = "test"; // ok
private final String i4; // not ok, never initialized
TestClass() {
i1 = "test1"; // ok
i2 = "test2"; // ok
i3 = "test3"; // not ok, overrides already set value
}
void mod() {
i1 = "test0"; // not ok, can't edit final i1
}
}
private final
and final private
has no difference. private final
和final private
没有区别。 For access modifiers the order does not matter.对于访问修饰符,顺序无关紧要。 So both of them will have similar behavior.
所以他们都会有类似的行为。
Try this:尝试这个:
public class Test {
private final String privateFinal;
final private String finalPrivate;
public Test() {
privateFinal = "Private Final";
finalPrivate = "Final Private";
}
}
The above code compiles absolutely fine.上面的代码编译得很好。
they're the same thing.它们是一样的。 private final > final private
私人决赛 > 最终私人
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.