Self-Learning Module
Outcomes Covered
- Draw and explain memory diagrams that illustrate the instantiation of objects
Watch
Do
Using the following, sketch what the stack and heap would like like after
PumpkinPatch.main()
is executed.
PumpkinPatch.java
package slm7;
public class PumpkinPatch {
public static void main(String[] args) {
String color1 = "Orange";
String color2 = "Green";
final double heavy = 4.5;
final double light = 2.4;
Pumpkin p1 = new Pumpkin(heavy, color1);
Pumpkin p2 = new Pumpkin(light, color2);
Pumpkin p3 = new Pumpkin(light, color1);
System.out.println("Pumpkin.numberOfPumkinsInPatch = " + Pumpkin.numberOfPumkinsInPatch);
}
}
Pumpkin.java
package slm7;
public class Pumpkin {
public static int numberOfPumkinsInPatch = 0;
public double weight;
public String color;
public Pumpkin(double weightIn, String colorIn){
this.weight = weightIn;
this.color = colorIn;
numberOfPumkinsInPatch++;
}
}
Submit your answer in Canvas.
Bonus Material (optional, but highly recommended)
- Dr. Taylor on How to Approach a Lab Assignment [3:47] [link]
- This is particularly relevant for the week 7 lab assignment.