私有最终静态属性与私有最终属性 [英] private final static attribute vs private final attribute

查看:45
本文介绍了私有最终静态属性与私有最终属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java 中,有什么区别:

In Java, what's the difference between:

private final static int NUMBER = 10;

private final int NUMBER = 10;

都是privatefinal,区别在于static属性.

Both are private and final, the difference is the static attribute.

什么更好?为什么?

推荐答案

一般来说,static 的意思是与类型本身相关联,而不是与实例相关联" 类型."

In general, static means "associated with the type itself, rather than an instance of the type."

这意味着您可以引用静态变量而无需创建该类型的实例,并且引用该变量的任何代码都引用完全相同的数据.将其与实例变量进行比较:在这种情况下,类的每个实例都有一个独立版本的变量.例如:

That means you can reference a static variable without having ever created an instances of the type, and any code referring to the variable is referring to the exact same data. Compare this with an instance variable: in that case, there's one independent version of the variable per instance of the class. So for example:

Test x = new Test();
Test y = new Test();
x.instanceVariable = 10;
y.instanceVariable = 20;
System.out.println(x.instanceVariable);

打印出10:y.instanceVariablex.instanceVariable 是分开的,因为xy 引用到不同的对象.

prints out 10: y.instanceVariable and x.instanceVariable are separate, because x and y refer to different objects.

可以通过引用来引用静态成员,尽管这样做是个坏主意.如果我们这样做:

You can refer to static members via references, although it's a bad idea to do so. If we did:

Test x = new Test();
Test y = new Test();
x.staticVariable = 10;
y.staticVariable = 20;
System.out.println(x.staticVariable);

然后会打印出 20 - 只有一个变量,而不是每个实例一个.把它写成这样会更清楚:

then that would print out 20 - there's only one variable, not one per instance. It would have been clearer to write this as:

Test x = new Test();
Test y = new Test();
Test.staticVariable = 10;
Test.staticVariable = 20;
System.out.println(Test.staticVariable);

这使得行为更加明显.现代 IDE 通常会建议将第二个列表更改为第三个.

That makes the behaviour much more obvious. Modern IDEs will usually suggest changing the second listing into the third.

没有理由像下面那样使用内联声明来初始化值,因为每个实例都有自己的 NUMBER 但总是具有相同的值(不可变并用文字初始化).这与所有实例只有一个 final static 变量相同.

There is no reason to have an inline declaration initializing the value like the following, as each instance will have its own NUMBER but always with the same value (is immutable and initialized with a literal). This is the same than to have only one final static variable for all instances.

private final int NUMBER = 10;

因此,如果它无法更改,则每个实例都没有一个副本.

Therefore if it cannot change, there is no point having one copy per instance.

但是,如果在这样的构造函数中初始化是有道理的:

But, it makes sense if is initialized in a constructor like this:

// No initialization when is declared
private final int number;

public MyClass(int n) {
   // The variable can be assigned in the constructor, but then
   // not modified later.
   number = n;
}

现在,对于 MyClass 的每个实例,我们可以有一个不同但不可变的 number 值.

Now, for each instance of MyClass, we can have a different but immutable value of number.

这篇关于私有最终静态属性与私有最终属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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