我的问题是关于静态关键字的一个特殊用法。可以使用静态关键字覆盖不属于任何函数的类中的代码块。例如下面的代码编译:
public class Test {
private static final int a;
static {
a = 5;
doSomething(a);
}
private static int doSomething(int x) {
return (x+5);
}
}
如果删除static关键字,它会报错,因为变量a是final。但是,可以同时删除最终关键字和静态关键字并使其编译。
这两方面都让我感到困惑。我怎么能有一个不属于任何方法的代码段呢?如何调用它呢?一般来说,这种用法的目的是什么?或者更好的是,我可以在哪里找到相关的文档?
带有静态修饰符的代码块表示类初始化式;如果没有静态修饰符,代码块就是一个实例初始化器。
类初始化器在装入类时(实际上是在解析类时,但这是一个技术术语)按照它们定义的顺序执行(自顶向下,就像简单的变量初始化器一样)。
实例初始化式按照类实例化时定义的顺序执行,即在构造函数代码执行之前,在超级构造函数调用之后。
如果你从int a中删除static,它就变成了一个实例变量,你不能从静态初始化块中访问它。这将导致编译失败,并报错“非静态变量a不能从静态上下文引用”。
如果你也从初始化程序块中删除了static,那么它就变成了实例初始化程序,因此int a在构造时就被初始化了。
当开发人员使用初始化式块时,Java编译器将初始化式复制到当前类的每个构造函数中。
例子:
以下代码:
class MyClass {
private int myField = 3;
{
myField = myField + 2;
//myField is worth 5 for all instance
}
public MyClass() {
myField = myField * 4;
//myField is worth 20 for all instance initialized with this construtor
}
public MyClass(int _myParam) {
if (_myParam > 0) {
myField = myField * 4;
//myField is worth 20 for all instance initialized with this construtor
//if _myParam is greater than 0
} else {
myField = myField + 5;
//myField is worth 10 for all instance initialized with this construtor
//if _myParam is lower than 0 or if _myParam is worth 0
}
}
public void setMyField(int _myField) {
myField = _myField;
}
public int getMyField() {
return myField;
}
}
public class MainClass{
public static void main(String[] args) {
MyClass myFirstInstance_ = new MyClass();
System.out.println(myFirstInstance_.getMyField());//20
MyClass mySecondInstance_ = new MyClass(1);
System.out.println(mySecondInstance_.getMyField());//20
MyClass myThirdInstance_ = new MyClass(-1);
System.out.println(myThirdInstance_.getMyField());//10
}
}
等价于:
class MyClass {
private int myField = 3;
public MyClass() {
myField = myField + 2;
myField = myField * 4;
//myField is worth 20 for all instance initialized with this construtor
}
public MyClass(int _myParam) {
myField = myField + 2;
if (_myParam > 0) {
myField = myField * 4;
//myField is worth 20 for all instance initialized with this construtor
//if _myParam is greater than 0
} else {
myField = myField + 5;
//myField is worth 10 for all instance initialized with this construtor
//if _myParam is lower than 0 or if _myParam is worth 0
}
}
public void setMyField(int _myField) {
myField = _myField;
}
public int getMyField() {
return myField;
}
}
public class MainClass{
public static void main(String[] args) {
MyClass myFirstInstance_ = new MyClass();
System.out.println(myFirstInstance_.getMyField());//20
MyClass mySecondInstance_ = new MyClass(1);
System.out.println(mySecondInstance_.getMyField());//20
MyClass myThirdInstance_ = new MyClass(-1);
System.out.println(myThirdInstance_.getMyField());//10
}
}
我希望开发人员能够理解我的例子。