Sobes.tech
Junior — Middle

Where in memory are static variables, methods, and classes located?

sobes.tech AI

Answer from AI

In Java, memory for different program elements is allocated as follows:

  • Static variables (static fields): stored in an area called "Metaspace" in modern JVMs or "PermGen" in older versions. This is where class metadata, including static fields, is stored.

  • Static methods: the methods themselves are stored in the memory area where class bytecodes are loaded (also in Metaspace). When a static method is called, the JVM accesses this area.

  • Classes: class information (metadata, bytecode) is loaded into Metaspace (starting from Java 8). Previously, PermGen was used.

Thus, static variables and methods are associated with the class and stored in the memory area allocated for class metadata, not in the heap where objects are stored.