Keyword
static, final, volatile
static vs. final
The static member can be accessed before the class object is created.
`static` is used to define the class member that can be used independently of any object of that class
Final keyword is used to declare, a constant variable, a method which can not be overridden and a class that can not be inherited.
import java.util.ArrayList;
import java.util.List;
class Test {
private final List foo;
public Test() {
foo = new ArrayList();
foo.add("foo"); // Modification-1
}
public void setFoo(List foo) {
//this.foo = foo; Results in compile time error.
}
}
volatile
在写入 volatile 变量时,之前写的(在缓存里的)变量都会被强制写入内存中
在读取 volatile 变量时,之前读的(在缓存里的)变量都会重新从内在中读取
Last updated
Was this helpful?