An object can contain references to other objects. If you declare these references as class/field variables then as the object itself is created on the heap, the values represented by the field references are stored on the heap.
So, if i have
public class A {
int size;
}
- I know that if size gets an int value of
2then that is stored as part of the object on the heap, but where is the reference i.e. the name size stored ? - Is the name “size” also stored inside of the object on the heap.
- How does JVM cross-reference
size == 2on the heap ? - When you load up the class it runs in the main thread and each thread will have its own stack. So these field references are not created on the main stack correct?
The name of the field is stored in the Class object
A.class. You can inspect class field names by using thejava.lang.reflectlibrary.For example, to inspect all the fields of a class, do this:
No. It is stored in
permgenmemoryIt looks up the field at compile time and the rest happens in bytecode
No. There are more memory areas than just heap and stack, There is also permgen, where the class definitions and class fields are stored. There still more memory areas, for example for the garbage collector.