Java 中静态代码块初始化问题测试 原创
情况一:变量是 static final 修饰的“编译期常量”,如 public static final String a = "JD";
public class Test {
public static void main(String[] args) {
System.out.println(Test2.a);
}
}
class Test2 {
public static final String a = "JD";
static {
System.out.print("OK");
}
}
参考答案
```
JD
```
JD
```
情况二:变量是 static final 修饰的“非编译期常量”,如 public static final String a = new String("JD");
public class Test {
public static void main(String[] args) {
System.out.println(Test2.a);
}
}
class Test2 {
public static final String a = new String("JD");
static {
System.out.print("OK");
}
}
参考答案
```
OKJD
```
OKJD
```
情况三:static 变量域不是 final,如 public static String a = "JD";
public class Test {
public static void main(String[] args) {
System.out.println(Test2.a);
}
}
class Test2 {
public static String a = "JD";
static {
System.out.print("OK");
}
}
参考答案
```
OKJD
```
OKJD
```
情况四:继承是JAVA语言的一个特性,针对类的继承,虚拟机会如何进行父类和子类的初始化加载呢?
public class Test {
public static void main(String[] args) {
System.out.print(B.c);
}
}
class A {
static {
System.out.print("A");
}
}
class B extends A {
static {
System.out.print("B");
}
public static final String c = "C";
}
参考答案
```
C
```
C
```
public class Test {
public static void main(String[] args) {
System.out.print(B.c);
}
}
class A {
static {
System.out.print("A");
}
}
class B extends A {
static {
System.out.print("B");
}
public static String c = "C";
}
参考答案
```
ABC
```
ABC
```
public class Test {
public static void main(String[] args) {
System.out.print(B.c);
}
}
class A {
static {
System.out.print("A");
}
}
class B extends A {
static {
System.out.print("B");
}
public static String c = new String("C");
}
参考答案
```
ABC
```
ABC
```
public class Test {
public static void main(String[] args) {
System.out.print(B.c);
}
}
class A {
static {
System.out.print("A");
}
}
class B extends A {
static {
System.out.print("B");
}
public static final String c = new String("C");
}
参考答案
```
ABC
```
ABC
```
public class Test {
public static void main(String[] args) {
System.out.print(B.c);
}
}
class A {
static {
System.out.print("A");
}
// 测试:只能选择其中一种一条语句
// public static final String c = "C";
// public static String c = "C";
// public static final String c = new String("C");
public static String c = new String("C");
}
class B extends A {
static {
System.out.print("B");
}
}
参考答案
```
public class Test {
public class Test {
public static void main(String[] args) {
System.out.print(B.c);
}
}
class A {
static {
System.out.print("A");
}
// 测试:只能选择其中一种一条语句
// public static final String c = "C"; // C
// public static String c = "C"; // AC
// public static final String c = new String("C"); // AC
public static String c = new String("C"); // AC
}
class B extends A {
static {
System.out.print("B");
}
}
</div>
<blockquote><b>总结:</b>
1、如果一个 <b>static final</b> 变量是<b>"编译期常量"</b>,就像 <i style="color:red;">public static final String a = "JD";</i>那样,那么这个值不需要对 Test2 类进行初始化就可以读取。
2、但是,如果只是将一个变量的域设置为 static 和 final 的,那<b>不足以</b>确保这种行为。例如,对 <i style="color:red;">public static final String a = new String("JD");</i>的访问将强制对 Test2 类进行初始化,因为<b>它不是</b>一个"编译期常量"。
3、如果一个 static 变量域不是 final,那么在对它进行访问时,总是要求在它被读取之前,要先进行<b>链接</b> (为这个域分配存储空间) 和<b>初始化</b> (初始化该存储空间) 就像 <i style="color:red;">public static String a = "JD";</i>。
</blockquote>
**参考链接**
* [https://blog.csdn.net/dreamzuora/article/details/80188708](https://blog.csdn.net/dreamzuora/article/details/80188708)