This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
Hi Folks, I want to know how java objects and primitives objects are laid out in memory. How static variables and objects are stored. For ex int a = 12; how memory allocation happens for variable a. Thanks in advance. Fred
"Fred", The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp. We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please edit your profile and select a new name which meets the requirements. Please be sure to change your display name soon. Accounts with invalid names get deleted. Thanks. Dave
Objects are stored on the heap. Static variables are stored with the Classfile. Variable references are stored in the stack related to the Frame. There is a frame for each method invoked. So local variables find themselves on the stack for the method that they are local to. The example that you gave shows the integer as a constant value. That value would be stored in the Constant Pool, with all the other constants and literals and Strings. You may enjoy reading the Java Virtual Machine Specifications for details on how such things work. .
"JavaRanch, where the deer and the Certified play" - David O'Meara