Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where instance variables(primitives) are stored in java? Is anyhow stack related to instance variables storage?

Tags:

java

variables

Where instance variables(primitives) are stored in java?

like image 570
Vipul J Avatar asked Dec 27 '22 14:12

Vipul J


2 Answers

Primitive variables are stored in the same places all variables are stored (including references):

  • Within objects created (allocated) on the heap, or
  • Within method stack frames as local variables, or
  • Within static areas of their containing class (which are on the heap).
like image 195
David R Tribble Avatar answered Mar 12 '23 04:03

David R Tribble


If you mean instance fields declared on a class, they are allocated on the heap as part of the object's own allocation.

Primitive (value type) variables declared as method locals are stored in the method's stack frame.

like image 23
cdhowie Avatar answered Mar 12 '23 02:03

cdhowie