Maj*_*ssi 4 java integer final class
一个final对象不能改变,但我们可以设置它的属性:
final MyClass object = new MyClass();
object.setAttr("something"); //<-- OK
object = someOtherObject; //<-- NOT OK
Run Code Online (Sandbox Code Playgroud)
有可能对a做同样的事情final Integer并改变它的int价值吗?
我问,因为我打电话给工人:
public SomeClass myFunction(final String val1, final Integer myInt) {
session.doWork(new Work() {
@Override
public void execute(...) {
//Use and change value of myInt here
//Using it requires it to be declared final (same reference)
}
}
Run Code Online (Sandbox Code Playgroud)
我需要设置myInt它内部的值.
我可以int在另一个类中声明我的内部,这样可行.但我想知道这是否必要.
Den*_*ret 13
不:a Integer是不可变的,就像例如String.
但是你可以设计自己的类来嵌入一个整数并使用它而不是整数:
public class MutableInteger {
private int value;
public MutableInteger(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
Run Code Online (Sandbox Code Playgroud)