java 类中静态块,在子类中执行Java静态块

I am preparing myself for Java certification test and I have found an interesting question related to the execution of Java static blocks. I have spent a lot of time reading about this topic, but I didn't find the answer I was looking for.

I know that static blocks are executed when the class is loaded into JVM or when the main method is invoked, but...

package oneClassTasks;

class Parent {

static int age;

}

class Child extends Parent {

static {

age = 5;

System.out.println("child's static block");

}

}

public class XXX {

public static void main(String args[]) {

System.out.println("Child age is : "+ Child.age);

}

}

The output is:

Child age is : 0

If I include verbose output with -verbose:class, then the output is:

...

[Loaded java.security.BasicPermissionCollection from C:\Program Files\Java\jre1.8.0_161\lib\rt.jar]

[Loaded oneClassTasks.XXX from file:/D:/temp/bin/]

[Loaded sun.launcher.LauncherHelper$FXHelper from C:\Program Files\Java\jre1.8.0_161\lib\rt.jar]

[Loaded java.lang.Class$MethodArray from C:\Program Files\Java\jre1.8.0_161\lib\rt.jar]

[Loaded java.lang.Void from C:\Program Files\Java\jre1.8.0_161\lib\rt.jar]

[Loaded oneClassTasks.Parent from file:/D:/temp/bin/]

[Loaded oneClassTasks.Child from file:/D:/temp/bin/]

Child age is : 0

[Loaded java.lang.Shutdown from C:\Program Files\Java\jre1.8.0_161\lib\rt.jar]

[Loaded java.lang.Shutdown$Lock from C:\Program Files\Java\jre1.8.0_161\lib\rt.jar]

We can see here that Child class is loaded into JVM.

Can someone explain why the static block from Child class is not executed?

解决方案

You saw that Child class was loaded, but it wasn't initialized.

Accessing Child.age doesn't cause the initialization of the Child class, since age is a member of Parent class. Therefore only Parent class is initialized, and age remains 0.

A class or interface type T will be initialized immediately before the

first occurrence of any one of the following:

T is a class and an instance of T is created.

A static method declared by T is invoked.

A static field declared by T is assigned.

A static field declared by T is used and the field is not a constant variable (§4.12.4).

T is a top level class (§7.6) and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed.

In your case you accessed a static field declared by Parent, so only Parent is initialized.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值