0

I learned that getting the value of a static field uses an offset from the beginning of the class data in the metaspace. (How exactly do static fields work internally?) But how does the jvm know the address of the class in metaspace?

I tried searching but couldn't find any information

EvMV
  • 1
  • 1
  • 1
    Well, the JVM is the one that put the class in metaspace. It'd be odd if it didn't know the location of the class. Or are you asking for the actual implementation? – Slaw Jun 07 '23 at 11:02
  • @Slaw Yes, I am asking for a rough implementation. Because classes can be placed in Metaspace in different order during execution. And when we write ClassName.staticPropertyName, JVM should get the address of ClassName in Metaspace. (As it was described in the link) – EvMV Jun 07 '23 at 11:17
  • Why are you asking? Are you having a problem or just curiosity? – aled Jun 07 '23 at 11:50
  • Just curious :) – EvMV Jun 07 '23 at 11:58
  • I don't know the specifics. [This page](https://wiki.openjdk.org/display/HotSpot/Metaspace) gives a high level overview of how Metaspace works. You can also look at [the source code](https://github.com/openjdk/jdk). Ultimately, the JVM must be keeping a pointer to the class data. – Slaw Jun 07 '23 at 12:40
  • @EvMV the JVM is a complex piece of code. Probably the overview that Slaw shared is as good as it gets. – aled Jun 07 '23 at 14:24
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jun 07 '23 at 15:06
  • 1
    *"getting the value of a static field uses an offset from the beginning of the class data in the metaspace"* - this is not correct. In HotSpot JVM, static fields are allocated at the end of the corresponding `java.lang.Class` instance in Java Heap. – apangin Jun 07 '23 at 19:49

0 Answers0