问题描述
如果我们将对象存储在一个对象的静态字段中,JVM如何为其分配内存?它是否存在于隐式"(不确定我使用的词是否正确)类对象中?静态字段与对象字段有何不同?
If we store objects in static fields of an object, how does the JVM allocate the memory for it? Does it live within "implicit"(not sure if I am using the right word) class object? How are static fields different from object fields?
推荐答案
静态字段是类变量,并在该类的所有实例之间共享.实例变量(或我认为您指的对象字段)属于类的单个实例,不共享.
Static fields are class variables, and are shared amongst all instances of that class. Instance variables (or object fields as I think you are referring to them as) belong to individual instances of a class and are not shared.
至于它们在内存中的存储位置将基于 JVM 的实现,并且没有理由要求两个不同的 JVM 根据规范将它们存储在同一位置(至少据我所知)- 应在此处插入适当的规格表链接).
As for where they are stored in memory is going to based on the implementation of the JVM and there is no reason two different JVMs would be required to store them in the same place by specification (to the best of my knowledge at least - should insert appropriate spec sheet link here).
这篇关于Java:静态字段在内存中的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!