Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于 ConstantValue 属性赋值给字段的时机问题 #187

Open
fanofxiaofeng opened this issue Aug 29, 2022 · 1 comment
Open

关于 ConstantValue 属性赋值给字段的时机问题 #187

fanofxiaofeng opened this issue Aug 29, 2022 · 1 comment

Comments

@fanofxiaofeng
Copy link

fanofxiaofeng commented Aug 29, 2022

在 第3版 《深入理解Java虚拟机》一书的 7.3.3 “准备” 这一小节里,有和 ConstantValue 相关的描述 (在第272页)
image

我在最新的 Java 虚拟机规范里看到了 ConstantValue 属性的相关介绍(具体位置是 4.7.2. The ConstantValue Attribute
image

我把关键的那句话复制过来了

If the ACC_STATIC flag in the access_flags item of the field_info structure is set, then the field represented by the field_info structure is assigned the value represented by its ConstantValue attribute as part of the initialization of the class or interface declaring the field (§5.5). This occurs prior to the invocation of the class or interface initialization method of that class or interface (§2.9.2).

从引文来看,用 ConstantValue 属性的值给字段赋值应该是发生在初始化(initialization)阶段(而非准备阶段),且其执行时间早于 <clinit>

@fanofxiaofeng fanofxiaofeng changed the title 关于 ConstantValue 的执行时机问题 关于 ConstantValue 属性赋值给字段的时机问题 Aug 29, 2022
@fanofxiaofeng
Copy link
Author

《深入理解Java虚拟机》 这本书给了我很多帮助,不过在这个小点上,似乎是有点表述错误

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant