Pages

Sunday 23 September 2012

Memory Management in Java

Instance variables vs Local variables

Instance variables represents the attributes for each instance(Object) of the class.

for every instance , same instance variables with different state(values).

Local variables are those variables which are local to method.
they are declared inside a method and also declared as a parameter to method.

Stack - is a place where all the local variables and method invocations live.

Heap - is a place where all the objects live.

observe the program below

package pack1;

public class Memory_Mgmt {
String s1;
int a;

public static void main(String[] args) {
new Memory_Mgmt();
show(10);

}
static void show(int j){
System.out.println(j);
}

}



check out the solution of memory allocation to the above program in the diagram given below.

Question

Where the reference variables will get stored stack or heap ?
( Memory_Mgmt m = new  Memory_Mgmt () , here m is the reference variable)

No comments:

Post a Comment