Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: where do static fields live within the memory?

Tags:

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?

like image 649
Midnight Blue Avatar asked Jan 26 '10 19:01

Midnight Blue


1 Answers

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.

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).

like image 178
Nick Larsen Avatar answered Sep 25 '22 02:09

Nick Larsen