Java 我们可以更改可变类的最终变量的值吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22351222/
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
Can we change the value of a final variable of a mutable class?
提问by Eagle
Following is a code where I have tried to change the value of a final variable. First it prints "a" then "ab". So, if we can change the value of a final variable then what's the benefit of declaring a variable as final? what is use of final key word then? Please any one can help me with this?????
以下是我尝试更改最终变量值的代码。首先它打印“a”然后是“ab”。那么,如果我们可以更改 final 变量的值,那么将变量声明为 final 有什么好处呢?那么final关键字有什么用呢?请任何人都可以帮我解决这个问题????
package test_new;
public class FinalVarValueChange {
public static void main(String[] args){
final StringBuffer s=new StringBuffer("a");
System.out.println("s before appending :"+s);
s.append("b");
System.out.println("s after appending :"+s);
}
}
采纳答案by Bathsheba
final
does not enforce any degree of constancy of the object to which a reference is referring (it is notthe same as const
in C++).
final
不强制到一个参考所指对象的恒常性的任何程度(这是不一样的const
在C ++)。
When you mark a variable as final
, it means that you cannot assign a different object reference to that variable.
当您将变量标记为 时final
,意味着您不能为该变量分配不同的对象引用。
For example, this code will not compile:
例如,此代码将无法编译:
final StringBuffer s=new StringBuffer("a");
s = new StringBuffer("another a"); /*not possible to reassign to s*/
It can be useful when using Runnables, Callables and locking idioms.
它在使用 Runnables、Callables 和锁定习惯用法时很有用。
回答by Abimaran Kugathasan
final variable/reference means, you can't reassign another value to that reference, but, you can change the state of the object.
final 变量/引用意味着,您不能为该引用重新分配另一个值,但是,您可以更改对象的状态。
Here, you are changing the StringBuffer
object s
internal state
在这里,您正在更改StringBuffer
对象s
内部状态
回答by Harmlezz
The final keyword ensures that the reference
, hold by the variable, won't change. The state
of the referenced instance may change. What you are likely look for is immutability
of the referenced instance. This is the responsibility of the instance.
final 关键字确保reference
由变量持有的 不会改变。该state
所引用的实例可能会改变。您可能要寻找的是immutability
引用的实例。这是实例的责任。
回答by AlexR
final
modifier means that you cannot change variable, i.e. reference to your object, i.e. assign other value to the same variable. You can however change the state of object itself.
final
修饰符意味着您不能更改变量,即对您的对象的引用,即为同一变量分配其他值。但是,您可以更改对象本身的状态。
回答by Shriram
The 'final' refers to the variable sb which is a reference to a StringBuffer. Because it is final, you cannot get sb to refer to another StringBuffer. You can however (as you have found) change the StringBuffer object that sb is currently referring to. Try compiling this code
“final”指的是变量 sb,它是对 StringBuffer 的引用。因为它是最终的,所以你不能让 sb 引用另一个 StringBuffer。但是,您可以(如您所见)更改 sb 当前所指的 StringBuffer 对象。尝试编译此代码
final StringBuffer sb = new StringBuffer();
sb.append("abc");
sb.append("xyz");
sb = new StringBuffer();
回答by Lawrence Liu
StringBuffer is mutable according to it's design, and method append is just a method to change it's content.
StringBuffer 根据其设计是可变的,方法 append 只是一种更改其内容的方法。
What final variable means in Java is:
最终变量在 Java 中的含义是:
For object, it means you can not assign another object to this reference, but it does not stops you from invoking methods of this object.
For basic type variables(int, float, boolean etc), since there's no reference and pointer, and object method here, so it simply means you can not change value of the variable.
对于对象,这意味着您不能将另一个对象分配给此引用,但它不会阻止您调用此对象的方法。
对于基本类型变量(int、float、boolean 等),因为这里没有引用和指针,以及对象方法,所以它只是意味着你不能改变变量的值。
Take the follow codes
获取以下代码
final ObjectA objA = new ObjectA();
You can of course invoke methods of this object like below
您当然可以调用此对象的方法,如下所示
objA.setXXX() //legal for a final variable objA
The side effect of setXXX() method is not controlled by final keyword here.
setXXX() 方法的副作用在这里不受 final 关键字控制。
But for the follow code
但是对于以下代码
final int a = 123;
You will not be able to assign any new value to this variable as below
您将无法为该变量分配任何新值,如下所示
a = 234; //Not legal if a has been defined as a final int variable.
But if you are defining it using Integer as below
但是,如果您使用 Integer 定义它,如下所示
final Integer a = new Integer(123);
You will be able to invoke methods of object a as below
您将能够调用对象 a 的方法,如下所示
a.xxx()
General speaking final definition is different for object and basic variable.
一般来说,对象和基本变量的最终定义是不同的。