Java中的最终和静态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4018851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
final and static in Java
提问by Ruba
I have read this sentence in a book but I didn't understand it:
我在一本书上看过这句话,但我不明白:
A field that is both static and final has only one piece of storage that cannot be changed.
一个既是静态又是最终的字段只有一块不能改变的存储。
Can anyone explain it for me?
谁能为我解释一下?
采纳答案by Mike Edwards
The source of your confusion may be that the word "static" in english and it's meaning in Java are only loosely related.
您混淆的根源可能是英语中的“静态”一词和它在 Java 中的含义只是松散相关的。
A variable defined in a class Cat in the "normal" way can be referred to as an instance variable.
以“正常”方式在类 Cat 中定义的变量可以称为实例变量。
class Cat {
int weight;
}
Each time you create a new object of type Cat, you create a new copy of the variable 'weight'. If you create 10 objects of type Cat, each one has it's own copy of the weight variable.
每次创建 Cat 类型的新对象时,都会创建变量“weight”的新副本。如果您创建 10 个 Cat 类型的对象,每个对象都有自己的权重变量副本。
A 'static' variable can be thought of as a class level variable, as opposed to an instance variable. A static variable has only one copy and belongs to the class Cat itself, rather than there being one copy for eachobject of type Cat.
一个“静态”变量可以被认为是一个类级别的变量,而不是一个实例变量。静态变量只有一个副本并且属于 Cat 类本身,而不是每个Cat 类型的对象都有一个副本。
class Cat {
static String speciesName;
int weight;
}
Here, no matter how many objects of type Cat we create, there is only one copy of speciesName.
在这里,无论我们创建了多少个 Cat 类型的对象,物种名称都只有一个副本。
If the static variable is also 'final,' than this one copy of the variable is the only piece of storage that cannot be changed. If the variable 'weight' were final in the above example, there would be 10 pieces of storage which could not be changed -- one for each object of type Cat that we had created.
如果静态变量也是“最终的”,那么这个变量的副本是唯一无法更改的存储。如果在上面的例子中变量“weight”是最终的,那么将有 10 个无法更改的存储空间——我们创建的每个 Cat 类型的对象都有一个。
回答by Jonas
A static
variable is common for all instances of the class. A final
variable can not change after it has been set the first time.
一个static
变量对于类的所有实例都是通用的。一个final
已被设定后的第一时间变量不能改变。
So a static final
variable in Java is common for all instances of the class, and it can not be changed after it has been set the first time.
所以static final
Java中的一个变量对于类的所有实例都是通用的,第一次设置后就不能再更改了。
class Car {
static final int numberOfWheels = 4;
Color color;
public Car(Color color) {
this.color = color;
}
}
Car redCar = new Car(Red);
Car blueCar = new Car(Blue);
Each car now has one individual and variable property color
and they share the property numberOfWheels
which can not be changed.
每辆车现在都有一个单独的可变属性color
,它们共享numberOfWheels
无法更改的属性。
回答by rmk
See the section 'Constants' for an explanation on this page:
有关此页面上的解释,请参阅“常量”部分:
http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html
http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html
回答by Vishwa
- Finalmeans cannot re-assign value to any variable
- Staticmeans only one copy of reference can be in whole in the class of all methods.
- final意味着不能为任何变量重新赋值
- 静态意味着在所有方法的类中只能有一个完整的引用副本。