This question is about the principles of memory management and garbage
collection for object oriented programming.
Throughout the following, we consider a single running example involving fragments
in a Java-like language; each snippet of Java should be considered to build this
running example further. (You will not be asked to write any code in your answers.)
In your answers, make reference to the stack, activation frame, method table, object
header, or heap as appropriate.
(a) Consider the following class definition.
class Animal {
List<String> sounds;
Randon mood;
}
public Animal () {
mood = new Random();
sounds = new LinkedList<String>(Arrays.asList("quiet"));
public String makeSound () {
String sound;
int index = mood.nextInt(sounds.size());
if (sounds.isEmpty()) sound = "";
else sound = sounds.get(index);
return sound; } }
At run-time, the instructions for the methods Animal () and makeSound() have to
be stored somewhere in memory.
i. Where in memory would the instructions for these methods be stored?
ii. How could this location be found at run-time, for an instance of to generate the
code for a method invocation?