Example with Static Variable Start class X { static int count = 0; private int data; public X(int n) { data = n; } // end int constructor public int getData() { return data; } // end getData } // end class X public class Z extends X { private Y a = new Y(3); public Z(int x, int y) { super(x); a.setValue(y); } // end int, int constructor public String toString () { return "From Z: " + a.getValue(); } // end toString public static void main(String[] args) { Z myZ = new Z (5, 7); Z myY = new Z (12, 14); System.out.println("value myZ: " + myZ); System.out.println("value myY: " + myY.a); } // end main } // end class Z Class Hierarchy – in memory Object X count Z Y Memory Map – instance vars class Y { private int value; public Y(int n) { value = n; } // end int constructor public void setValue(int z) { value = z; } // end method getValue public int getValue() { return value; } // end method getValue public String toString () { return "From Y: " + value; } // end toString } // end class Y mvZ X.data 5 a 7 mvY Y.value 12 a 14
Example with Static Variable - 1 Initial class X { static int count = 0; private int data; public X(int n) { data = n; } // end int constructor public int getData() { return data; } // end getData } // end class X public class Z extends X { private Y a = new Y(3); public Z(int x, int y) { super(x); a.setValue(y); } // end int, int constructor public String toString () { return "From Z: " + a.getValue(); } // end toString public static void main(String[] args) { Z myZ = new Z (5, 7); Z myY = new Z (12, 14); System.out.println("value myZ: " + myZ); System.out.println("value myY: " + myY.a); } // end main } // end class Z Class Hierarchy – in memory Object X count Z Y Memory Map – instance vars class Y { private int value; public Y(int n) { value = n; } // end int constructor public void setValue(int z) { value = z; } // end method getValue public int getValue() { return value; } // end method getValue public String toString () { return "From Y: " + value; } // end toString } // end class Y Comments: Before the program starts, but after it’s loaded into memory, the class hierarchy is loaded and the static variables are instantiated as part of the class load.
Example with Static Variable - 2 One class X { static int count = 0; private int data; public X(int n) { data = n; } // end int constructor public int getData() { return data; } // end getData } // end class X public class Z extends X { private Y a = new Y(3); public Z(int x, int y) { super(x); a.setValue(y); } // end int, int constructor public String toString () { return "From Z: " + a.getValue(); } // end toString public static void main(String[] args) { Z myZ = new Z (5, 7); Z myY = new Z (12, 14); System.out.println("value myZ: " + myZ); System.out.println("value myY: " + myY.a); } // end main } // end class Z Class Hierarchy – in memory Object X count Z Y Memory Map – instance vars class Y { private int value; public Y(int n) { value = n; } // end int constructor public void setValue(int z) { value = z; } // end method getValue public int getValue() { return value; } // end method getValue public String toString () { return "From Y: " + value; } // end toString } // end class Y mvZ X.data 5 a 7 Comments: The line just executed accomplished the following – all the code shown in red is involved: Reserved memory for the mvZ handle Reserved memory for a Z instance Reserved memory for a Y instance Inited X.data to 5 in this instance Inited Y.value to 7 for this Y instance Returned handle to new Y instance, setting value Z.a Same for Z and mvZ Y.value
Example with Static Variable Two class X { static int count = 0; private int data; public X(int n) { data = n; } // end int constructor public int getData() { return data; } // end getData } // end class X public class Z extends X { private Y a = new Y(3); public Z(int x, int y) { super(x); a.setValue(y); } // end int, int constructor public String toString () { return "From Z: " + a.getValue(); } // end toString public static void main(String[] args) { Z myZ = new Z (5, 7); Z myY = new Z (12, 14); System.out.println("value myZ: " + myZ); System.out.println("value myY: " + myY.a); } // end main } // end class Z Class Hierarchy – in memory Object X count Z Y Memory Map – instance vars class Y { private int value; public Y(int n) { value = n; } // end int constructor public void setValue(int z) { value = z; } // end method getValue public int getValue() { return value; } // end method getValue public String toString () { return "From Y: " + value; } // end toString } // end class Y mvZ X.data 5 a 7 Comments: The line just executed accomplished the following – all the code shown in red is involved As before for mvY mvY Y.value 12 a 14
Example with Static Variable Three class X { static int count = 0; private int data; public X(int n) { data = n; } // end int constructor public int getData() { return data; } // end getData } // end class X public class Z extends X { private Y a = new Y(3); public Z(int x, int y) { super(x); a.setValue(y); } // end int, int constructor public String toString () { return "From Z: " + a.getValue(); } // end toString public static void main(String[] args) { Z myZ = new Z (5, 7); Z myY = new Z (12, 14); System.out.println("value myZ: " + myZ); System.out.println("value myY: " + myY.a); } // end main } // end class Z Class Hierarchy – in memory Object X count Z Y Memory Map – instance vars class Y { private int value; public Y(int n) { value = n; } // end int constructor public void setValue(int z) { value = z; } // end method getValue public int getValue() { return value; } // end method getValue public String toString () { return "From Y: " + value; } // end toString } // end class Y mvZ X.data 5 a 7 Comments: The line just executed accomplished the following – all the code shown in red is involved This line of code uses toString from Z, which then calls getValue in Y mvY Y.value 12 a 14
Example with Static Variable Four class X { static int count = 0; private int data; public X(int n) { data = n; } // end int constructor public int getData() { return data; } // end getData } // end class X public class Z extends X { private Y a = new Y(3); public Z(int x, int y) { super(x); a.setValue(y); } // end int, int constructor public String toString () { return "From Z: " + a.getValue(); } // end toString public static void main(String[] args) { Z myZ = new Z (5, 7); Z myY = new Z (12, 14); System.out.println("value myZ: " + myZ); System.out.println("value myY: " + myY.a); } // end main } // end class Z Class Hierarchy – in memory Object X count Z Y Memory Map – instance vars class Y { private int value; public Y(int n) { value = n; } // end int constructor public void setValue(int z) { value = z; } // end method getValue public int getValue() { return value; } // end method getValue public String toString () { return "From Y: " + value; } // end toString } // end class Y mvZ X.data 5 a 7 Comments: The line just executed accomplished the following – all the code shown in red is involved Here the call is to the toString method in Y mvY Y.value 12 a 14