1 / 6

Start

Start. 14. 7. 12. 5. a. a. Example with Static Variable. 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.

Télécharger la présentation

Start

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Start 14 7 12 5 a a Example with Static Variable 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 mvY Y.value

  2. Initial Example with Static Variable - 1 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.

  3. One 7 5 a Example with Static Variable - 2 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 • 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

  4. Two 14 7 12 5 a a Example with Static Variable 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 Comments: The line just executed accomplished the following – all the code shown in red is involved As before for mvY mvY Y.value

  5. Three 14 7 12 5 a a Example with Static Variable 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 constructorpublic 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 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

  6. Four 14 7 12 5 a a Example with Static Variable 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 getValuepublic String toString () { return "From Y: " + value; } // end toString} // end class Y mvZ X.data 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

More Related