When creating 2 objects of the same type, will the handle from the stack memory point to the same object in the heap or will it point to 2 separate objects. For clarity here is the specific question…
class Q2 {
private static int num = 0;
private String prefix;
public Q2 (String p)
{ prefix = p; }
public String Msg (String str) {
String n;
num++;
n = num.ToString();
return n + " - " + prefix + str;
}
}
Using an appropriate diagram, describe the state of memory after all of the following statements have been executed.
Q2 var1, var2;
var1 = new Q2("Question 2");
var2 = new Q2 ("Another view");
Here are the answers I cannot decide between:
1 object:

2 objects:

You are using the
newkeyword to instantiate objects in two separate variables, so, this always creates new object on the heap. So the answer would be that it will always point to two separate objects.EDIT:
Static variables such as
numare stored in a special area on the heap called High Frequency Heap, that is not being collected by the garbage collector etc.