Think of it this way, the location of an object in memory is determined by the type where it was and where it was declared. If the object is a value type, its value is stored where you specified the variable. If the object is a reference type, its reference is stored where you declared the variable, while the actual instance of the object exists on the heap.
When you declare a local variable, you declare a variable on the stack. Therefore, a value of type value will be on the stack. The reference to the reference type will be on the stack, and the instance of the object is still on the heap.
If you declare an instance variable in a class (reference type), you effectively declare instance variables on the heap. A value of type value will be on the heap (in the instance of the object). The link of the reference type will also be on the heap (in the instance of the object), the instance of the object will be in another place on the heap.
If you declare an instance variable in the structure (value type) where it is located depends on where the base structure was declared.
In the case of an int int[] array, arrays are reference types, and you can think of int values ββdeclared as "fields" for this type so that your integers are efficient on the heap.
source share