1 / 226

Chapter Topics

Chapter Topics. Chapter 8 discusses the following main topics: Static Class Members Passing Objects as Arguments to Methods Returning Objects from Methods The toString method Writing an equals Method Methods that Copy Objects. Chapter Topics.

ahartsock
Download Presentation

Chapter Topics

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. Chapter Topics Chapter 8 discusses the following main topics: Static Class Members Passing Objects as Arguments to Methods Returning Objects from Methods The toString method Writing an equals Method Methods that Copy Objects

  2. Chapter Topics Chapter 8 discusses the following main topics: Aggregation The this Reference Variable Enumerated Types Garbage Collection Focus on Object-Oriented Design: Class Collaboration

  3. review import java.util.Scanner; … Scanner keyboard = new Scanner(System.in); … //The variable name is of String class data type. String name = “John Smith”; /*create a String object in the memory, initialized with the string literal “John Smith”. The object is referenced by the name variable.*/ or System.out.print(“What is your name? “”); name = keyboard.nextline(); … int stringSize = name.length(); /*the length method returns number of characters in the string “John Smith” (which is 10) */

  4. import java.util.Scanner; … Scanner keyboard = new Scanner(System.in); … //The variable name is of String class data type. String name = “John Smith”; /*create a String object in the memory, initialized with the string literal “John Smith”. The object is referenced by the name variable.*/ or System.out.print(“What is your name?”); name = keyboard.nextline(); … String name = new String(“John Smith”); /*We can use new operator to create a String object, and initialize the object by passing a string literal to the constructor.*/ review

  5. package ch08_ClassnObjectDemos01; import java.util.Scanner; publicclass Ch08_classesNobjectsDemos01 { publicstaticvoid main(String[] args) { String name = "John Everest"; String strformat = "The name is %s and the length is %d.\n"; System.out.printf(strformat, name, name.length()); Scanner kb = new Scanner(System.in); System.out.print("Print your name?"); name = kb.nextLine(); System.out.printf(strformat, name, name.length()); String personName = new String("Dragon fly."); System.out.printf(strformat, personName, personName.length()); String animalName = new String(); System.out.print("Give the animalName?"); animalName = kb.nextLine(); System.out.printf(strformat, animalName, animalName.length()); animalName = "The world will have generation of idiots"; System.out.printf(strformat, animalName, animalName.length()); }//end of main }

  6. The name is John Everest and the length is 12. Print your name?Albert Einstein The name is Albert Einstein and the length is 15. The name is Dragon fly. and the length is 11. Give the animalName? I fear the day that the technology will surpass out human interaction The name is I fear the day that the technology will surpass out human interaction and the length is 70. The name is The world will have generation of idiots and the length is 40.

  7. name John Smith String name = “John Smith”; String name = new String(“John Smith)”; String[] names = { "Bill", "Susan", "Steven", "Jean ", " Kaye "}; Bill names[0] names.length is 5 Susan names[1] names[2] Steven names[4].length() is 6 names[3] names[4] Jean Kaye

  8. The length Field & The length Method – recalled from #7 An array of String objects can be created: String[] names = { "Bill", "Susan", "Steven", "Jean ", " Kaye "}; • Arrays have a final field named length of 5. • String objects have a method named length. • To display the length of each string held in a String array: for (inti = 0; i < names.length; i++) System.out.println(names[i].length()); • An array’s length member is a field • You do not write a set of parentheses after its name. • A String’s length is a method • You do write the parentheses after the name of the String class’s length method.

  9. package ch08_Classes_Objects_01; publicclass Chap08_LengthField_Method { publicstaticvoid main(String[] args) { String[] names = { "Bill", "Susan", "Steven", "Jean ", " Kaye "}; //Arrays have a final field named length of 5. //An array’s length member is a field System.out.println(names.length); for (inti = 0; i < names.length; i++) //String objects have a method named length(). //A String’s length is a method System.out.println(names[i].length()); }//end of main } Output (in terms of bytes is 5 – array size 4 5 6 5 6

  10. package ch08_ClassnObjectDemos02; publicclass Ch08_classesAndObjectsDemos02 { publicstaticvoid main(String[] args) { String strformat = "The name is %s and the length is %d.\n"; String[] names = {"Mount Everest", "Albert Einstein", "Dragon Fly", "I fear the day that the technology will surpass our human interaction.", "The world will have generation of idiots."}; System.out.println("The length of the String[] names is " + names.length); for (inti = 0; i < names.length; i++) { System.out.printf(strformat, names[i], names[i].length()); }//end for }//end of main }

  11. The length of the String[] names is 5 The name is Mount Everest and the length is 13. The name is Albert Einstein and the length is 15. The name is Dragon Fly and the length is 10. The name is I fear the day that the technology will surpass our human interaction, and the length is 70. The name is The world will have generation of idiots. and the length is 41.

  12. Review of Instance Fields and Methods Each instance of a class has its own copy of instance variables. Example: The Rectangle class defines a length and a widthfields. Each instance of the Rectangle class can have different values stored in its length and widthfields. Instance methods require that an instance of a class be created in order to be used. As an example, box2.getLength() Instance methods typically interact with instancefields or calculate values based on those fields. Rectangle box1 = new Rectangle(12.0, 3.0); Rectangle box2 = new Rectangle(3.0, 12.1);

  13. package ch08_Classes_Objects_01; public class Rectangle { private double rectLength; //field private double rectWidth; //field public Rectangle() { //constructor rectLength = 0.0; rectWidth = 0.0; } public Rectangle(double len, double w){ //constructor rectLength = len; rectWidth = w; } public double getLength(){ //Rectangle object’s method return rectLength; } public double getWidth(){ return rectWidth; } public void setLength(double len) { rectLength = len; } public void setWidth(double w){ rectWidth = w; } }

  14. package ch08_Classes_Objects_01; publicclass Chap08_LengthField_Method { publicstaticvoid main(String[] args) { String str = " has length %.2f and width %.2f.\n"; Rectangle box1 = new Rectangle(12.0, 3.0); System.out.printf("box1"+ str, box1.getLength(), box1.getWidth()); Rectangle box2 = new Rectangle(3.0, 12.1); Rectangle box3 = new Rectangle(); box1 = new Rectangle(); box1.setLength(11.0); box1.setWidth(11.1); System.out.printf("box1"+ str, box1.getLength(), box1.getWidth()); System.out.printf("box2"+ str, box2.getLength(), box2.getWidth()); System.out.printf("box3"+ str, box3.getLength(), box3.getWidth()); }//end of main }

  15. package ch08_Classes_Objects_01; publicclass Chap08_LengthField_Method { publicstaticvoid main(String[] args) { String str = " has length %.2f and width %.2f.\n"; Rectangle box1 = new Rectangle(12.0, 3.0); System.out.printf("box1"+ str, box1.getLength(), box1.getWidth()); Rectangle box2 = new Rectangle(3.0, 12.1); Rectangle box3 = new Rectangle(); box1 = new Rectangle(); box1.setLength(11.0); box1.setWidth(11.1); System.out.printf("box1"+ str, box1.getLength(), box1.getWidth()); System.out.printf("box2"+ str, box2.getLength(), box2.getWidth()); System.out.printf("box3"+ str, box3.getLength(), box3.getWidth()); }//end of main } The output is: box1 has length 12.00 and width 3.00. box1 has length 11.00 and width 11.10. box2 has length 3.00 and width 12.10. box3 has length 0.00 and width 0.00.

  16. The output is: box1 has length 12.00 and width 3.00. box1 has length 11.00 and width 11.10. box2 has length 3.00 and width 12.10. box3 has length 0.00 and width 0.00.

  17. Static Class Members Static fields and static methodsdo not belong to a single instance of a class. To invoke a static method or use a static field, use the class name, rather than the instance name. Example: double val = Math.sqrt(25.0); names.length names[i].length() box1 = new Rectangle(); box1.setLength(11.0); As an example, Rectangle.getCountBoxesCreatedstatic() Class name Static method

  18. package ch08_ClassInstanceFM; public class Rectangle { private double rectLength; private double rectWidth; private static double rectHeight = 10.0; //class field and must be static private double ns_rectHeight = 100.0;//instance field and cannot be static public Rectangle() { rectLength = 0.0; rectWidth = 0.0; } public Rectangle(double len, double w){ rectLength = len; rectWidth = w; } public double getLength(){ return rectLength; } public double getWidth(){ return rectWidth; } public double ns_getHeight(){ return ns_rectHeight; } public static double getHeight(){ return rectHeight; } //static method public void setLength(double len){ rectLength = len; } public void setWidth(double w){ rectWidth = w; } public static void setHeight(double h){ rectHeight = rectHeight + h; } //static method public void setHeight(double h){ ns_rectHeight = ns_rectHeight + h; } }

  19. package ch08_ClassInstanceFM; public class Chap08_ClassFieldsMethods { public static void main(String[] args) { String str = " has length %.2f, width %.2f and height %.2f.\n"; Rectangle box1 = new Rectangle(12.0, 3.0); System.out.printf("box1"+ str, box1.getLength(), box1.getWidth(), box1.ns_getHeight()); Rectangle box2 = new Rectangle(3.0, 12.1); System.out.printf("box2"+ str, box2.getLength(), box2.getWidth(), Rectangle.getHeight()); Rectangle box3 = new Rectangle(); box1 = new Rectangle(); box1.setLength(11.0); box1.setWidth(11.1); Rectangle.setHeight(22.2); //add 22.2 to the given height System.out.printf("box1"+ str, box1.getLength(), box1.getWidth(), Rectangle.getHeight()); Rectangle.setHeight(22.2); //add 22.2 to the height. System.out.printf("box2"+ str, box2.getLength(), box2.getWidth(), Rectangle.getHeight()); System.out.printf("box3"+ str, box3.getLength(), box3.getWidth(), Rectangle.getHeight()); }//end of main } box1 has length 12.00, width 3.00 and height 100.00. box2 has length 3.00, width 12.10 and height 10.00. box1 has length 11.00, width 11.10 and height 32.20. box2 has length 3.00, width 12.10 and height 54.40. box3 has length 0.00, width 0.00 and height 54.40.

  20. The output is: box1 has length 12.00, width 3.00 and height 100.00. box2 has length 3.00, width 12.10 and height 10.00. box1 has length 11.00, width 11.10 and height 32.20. box2 has length 3.00, width 12.10 and height 54.40. box3 has length 0.00, width 0.00 and height 54.40.

  21. package ch08_ClassInstanceFM; publicclass Chap08_ClassFieldsethods { publicstaticvoid main(String[] args) { String str = " has length %.2f, width %.2f and height %.2f.\n"; Rectangle box1 = new Rectangle(12.0, 3.0); System.out.printf("T01a_box1"+ str, box1.getLength(), box1.getWidth(), Rectangle.getHeight()); System.out.printf("T01b_box1"+ str, box1.getLength(), box1.getWidth(), box1.ns_getHeight()); Rectangle box2 = new Rectangle(3.0, 12.1); System.out.printf("T02a_box2"+ str, box2.getLength(), box2.getWidth(), Rectangle.getHeight()); System.out.printf("T02b_box2"+ str, box2.getLength(), box2.getWidth(), box2.ns_getHeight()); Rectangle box3 = new Rectangle(); System.out.printf("T03a_box3"+ str, box3.getLength(), box3.getWidth(), Rectangle.getHeight()); System.out.printf("T03b_box3"+ str, box3.getLength(), box3.getWidth(), box3.ns_getHeight()); System.out.println("\n");

  22. box1 = new Rectangle(); box1.setLength(11.0); box1.setWidth(11.1); Rectangle.setHeight(22.2); //add 22.2 to the given height 10.0 box1.ns_setHeight(10.10); box1.ns_setHeight(10.20); //add 10.10 + 10.20 to the given height 0 System.out.printf("T04a_box1"+ str, box1.getLength(), box1.getWidth(), Rectangle.getHeight()); System.out.printf("T04b_box1"+ str, box1.getLength(), box1.getWidth(), box1.ns_getHeight()); Rectangle.setHeight(22.2); //add 22.2 to the height. box2.ns_setHeight(10.10); System.out.printf("T05a_box2"+ str, box2.getLength(), box2.getWidth(), Rectangle.getHeight()); System.out.printf("T05b_box2"+ str, box2.getLength(), box2.getWidth(), box2.ns_getHeight()); System.out.printf("T06a_box3"+ str, box3.getLength(), box3.getWidth(), Rectangle.getHeight()); System.out.printf("T06a_box3"+ str, box3.getLength(), box3.getWidth(), box3.ns_getHeight()); System.out.println("\n");

  23. Rectangle.setHeight(33.30); //add 33.3 to the given height 54.4 box1.ns_setHeight(33.33); //add 33.33 to the given height 20.30 System.out.printf("T07a_box1"+ str, box1.getLength(), box1.getWidth(), Rectangle.getHeight()); System.out.printf("T07b_box1"+ str, box1.getLength(), box1.getWidth(), box1.ns_getHeight()); Rectangle.setHeight(10.21); //add 22.2 to the height. box2.ns_setHeight(10.13); System.out.printf("T08a_box2"+ str, box2.getLength(), box2.getWidth(), Rectangle.getHeight()); System.out.printf("T08b_box2"+ str, box2.getLength(), box2.getWidth(), box2.ns_getHeight()); System.out.printf("T09a_box3"+ str, box3.getLength(), box3.getWidth(), Rectangle.getHeight()); System.out.printf("T09a_box3"+ str, box3.getLength(), box3.getWidth(), box3.ns_getHeight()); System.out.println("\n"); }//end of main }

  24. package ch08_ClassInstanceFM; publicclass Rectangle { privatedoublerectLength; privatedoublerectWidth; privatestaticdoublerectHeight = 10.0; //static field privatedoublens_rectHeight; public Rectangle() { rectLength = 0.0; rectWidth = 0.0; } public Rectangle(doublelen, doublew){ rectLength = len; rectWidth = w; } publicdouble getLength(){ returnrectLength; } publicdouble getWidth(){ returnrectWidth; } publicdouble ns_getHeight(){ returnns_rectHeight; } publicstaticdouble getHeight(){ returnrectHeight; } //static method publicvoid setLength(doublelen){ rectLength = len; } publicvoid setWidth(doublew){ rectWidth = w; } publicstaticvoid setHeight(doubleh){ rectHeight = rectHeight + h; } //static method publicvoid ns_setHeight(doubleh){ ns_rectHeight = ns_rectHeight + h; } }

  25. T01a_box1 has length 12.00, width 3.00 and height 10.00. T01b_box1 has length 12.00, width 3.00 and height 0.00. T02a_box2 has length 3.00, width 12.10 and height 10.00. T02b_box2 has length 3.00, width 12.10 and height 0.00. T03a_box3 has length 0.00, width 0.00 and height 10.00. T03b_box3 has length 0.00, width 0.00 and height 0.00. T04a_box1 has length 11.00, width 11.10 and height 32.20. T04b_box1 has length 11.00, width 11.10 and height 20.30. T05a_box2 has length 3.00, width 12.10 and height 54.40. T05b_box2 has length 3.00, width 12.10 and height 10.10. T06a_box3 has length 0.00, width 0.00 and height 54.40. T06a_box3 has length 0.00, width 0.00 and height 0.00. T07a_box1 has length 11.00, width 11.10 and height 87.70. T07b_box1 has length 11.00, width 11.10 and height 53.63. T08a_box2 has length 3.00, width 12.10 and height 97.91. T08b_box2 has length 3.00, width 12.10 and height 20.23. T09a_box3 has length 0.00, width 0.00 and height 97.91. T09a_box3 has length 0.00, width 0.00 and height 0.00.

  26. The static field is shared by all the instances of the class. For example, Consider public class Rectangle { private static double height = 10.00 private double ns_height; publicstaticvoid setHeight(doubleh) { rectHeight = rectHeight + h; } publicvoid ns_setHeight(doubleh){ ns_rectHeight = ns_rectHeight + h; } … } The values of static field, height, are shared by all instances of the class. Rectangle.setHeight(10.25); Each instance, such as box1, box2 and box3 of the class has their own value of non-static field, ns_height. box1.ns_setHeight(10.25); box2.ns_setHeight(10.00);

  27. Another example:

  28. package ch08_CountingObjects_01; public class Counting { private static int instanceCount = 0; private int instanceCountNonStatic = 0; public Counting() { instanceCount++; instanceCountNonStatic++; } public int getInstanceCount(){return instanceCount; } public static int getInstanceCt() {return instanceCount; } public int getInstanceCountNonStatic() { return instanceCountNonStatic; } }

  29. package Ch08_CountObjects_01; publicclass Chap08_Count_Objects { publicstaticvoid main(String[] args) { intobjectCount; //Create Five instances of the Countable class Counting object00; displayObjectCount(Counting.getInstanceCt());//not need instance of the class. InstanceCount = 0. //displayObjectCount(object00.getInstanceCount());//Error: need to initialize object00 //displayObjectCount(object00.getInstanceCountNonStatic());//Error: likewise object00 = new Counting();//create 1 object00 instance of the Class. displayObjectCount(Counting.getInstanceCt());// InstanceCount = 1. displayObjectCount(object00.getInstanceCount());//need instance of the class. InstanceCount = 1. displayObjectCount(object00.getInstanceCountNonStatic());// InstanceCountNonStatic = 1. System.out.println(""); Counting object01 = new Counting();//create 1 object01 instance of the Class displayObjectCount(Counting.getInstanceCt()); //total InstanceCount (00, 01) = 1+1 = 2 System.out.println("\tAt Point 01: After creating an object01: " + "\n\tUse getInstanceCt method to get value of static field" + "\n\tUse getInstance method to get value of static field" + "\n\tand getInstanceCountNonStatic method to get non-static field"); displayObjectCount(Counting.getInstanceCt()); //total InstanceCount (00, 01) = 1+1 = 2 objectCount = object01.getInstanceCount(); displayObjectCount(objectCount);//InstantCount (00, 01) = 1+1=2 displayObjectCount(object00.getInstanceCountNonStatic());//InstanceCountNonStatic(00) = 1 displayObjectCount(object01.getInstanceCountNonStatic());//InstanceCountNonStatic(01) = 1 System.out.println(""); object01 = new Counting();//create 1 object 01 instance of the Class again. //objectCount = object01.getInstanceCount(); System.out.println("\tAt Point 02: After creating an object01 again: " + "\n\tUse getInstanceCt method to get value of static field" + "\n\tUse getInstance method to get value of static field" + "\n\tand getInstanceCountNonStatic method to get non-static field"); //displayObjectCount(objectCount); displayObjectCount(Counting.getInstanceCt()); //total InstanceCount (00,01,01) = 2+1 = 3 displayObjectCount(object01.getInstanceCount());//total InstanceCount (00,01,01) = 2+1 = 3 displayObjectCount(object00.getInstanceCount());//total InstanceCount (00,01,01) = 2+1 = 3 displayObjectCount(object01.getInstanceCountNonStatic());//InstanceCountNonStatic (01) = 1 displayObjectCount(object00.getInstanceCountNonStatic());//InstanceCountNonStatic (00) = 1 System.out.println(""); object01 = new Counting();//create 1 object 01 instance of the Class again. //objectCount = object01.getInstanceCount(); System.out.println("\tAt Point 03: After creating the third object01: " + "\n\tUse getInstanceCt method to get value of static field" + "\n\tUse getInstance method to get value of static field" + "\n\tand getInstanceCountNonStatic method to get non-static field"); //displayObjectCount(objectCount); displayObjectCount(Counting.getInstanceCt()); //total InstanceCount (00,01,01,01) = 3+1 = 4 displayObjectCount(object01.getInstanceCount());//total InstanceCount (00,01,01,01) = 3+1 = 4 displayObjectCount(object00.getInstanceCount());//total InstanceCount (00,01,01,01) = 3+1 = 4 displayObjectCount(object01.getInstanceCountNonStatic());//InstanceCountNonStatic (01) = 1 displayObjectCount(object00.getInstanceCountNonStatic());//InstanceCountNonStatic (00) = 1 System.out.println(""); Counting object02 = new Counting();//Create 1 object 02 instance of the Class. //objectCount = object02.getInstanceCount(); System.out.println("\tAt Point 04: After creating an object02: " + "\n\tUse getInstanceCt method to get value of static field" + "\n\tUse getInstance method to get value of static field" + "\n\tand getInstanceCountNonStatic method to get non-static field"); //displayObjectCount(objectCount); displayObjectCount(Counting.getInstanceCt());//total InstanceCount (00,01,01,01,02) = 4+1 = 5 displayObjectCount(object02.getInstanceCount());//total InstanceCount (00,01,01,01,02) = 4+1 = 5 displayObjectCount(object01.getInstanceCount());//total InstanceCount (00,01,01,01,02) = 4+1 = 5 displayObjectCount(object00.getInstanceCount());//total InstanceCount (00,01,01,01,02) = 4+1 = 5 displayObjectCount(object02.getInstanceCountNonStatic());//InstanceCountNonStatic (02) = 1 displayObjectCount(object01.getInstanceCountNonStatic());//InstanceCountNonStatic (01) = 1 displayObjectCount(object00.getInstanceCountNonStatic());//InstanceCountNonStatic (00) = 1 }//end of main publicstaticvoid displayObjectCount(intnumberObjectCount) { System.out.println("\t" + numberObjectCount + " instances of the class was created."); }//end of displayObjectCount }

  30. package Ch08_CountObjects_01; publicclass Chap08_Count_Objects { publicstaticvoid main(String[] args) { intobjectCount; //Create Five instances of the Countable class Counting object00; displayObjectCount(Counting.getInstanceCt()); //not need instance of the class. InstanceCount = 0. //displayObjectCount(object00.getInstanceCount());//Error: need to initialize object00 //displayObjectCount(object00.getInstanceCountNonStatic());//Error: likewise object00 = new Counting(); //create 1 object00 instance of the Class. displayObjectCount(Counting.getInstanceCt()); // InstanceCount = 1. displayObjectCount(object00.getInstanceCount()); //need instance of the class. InstanceCount = 1. displayObjectCount(object00.getInstanceCountNonStatic()); // InstanceCountNonStatic = 1. System.out.println("");

  31. Counting object01 = new Counting(); //create 1 object01 instance of the Class displayObjectCount(Counting.getInstanceCt()); //total InstanceCount (00, 01) = 1+1 = 2 System.out.println("\tAt Point 01: After creating an object01: " + "\n\tUse getInstanceCt method to get value of static field" + "\n\tUse getInstance method to get value of static field" + "\n\tand getInstanceCountNonStatic method to get non-static field"); displayObjectCount(Counting.getInstanceCt()); //total InstanceCount (00, 01) = 1+1 = 2 objectCount = object01.getInstanceCount(); displayObjectCount(objectCount); //InstantCount (00, 01) = 1+1=2 displayObjectCount(object00.getInstanceCountNonStatic()); //InstanceCountNonStatic(00) = 1 displayObjectCount(object01.getInstanceCountNonStatic()); //InstanceCountNonStatic(01) = 1 System.out.println("");

  32. object01 = new Counting(); //create 1 object 01 instance of the Class again. System.out.println("\tAt Point 02: After creating an object01 again: " + "\n\tUse getInstanceCt method to get value of static field" + "\n\tUse getInstance method to get value of static field" + "\n\tand getInstanceCountNonStatic method to get non-static field"); displayObjectCount(Counting.getInstanceCt()); //total InstanceCount (00,01,01) = 2+1 = 3 displayObjectCount(object01.getInstanceCount()); //total InstanceCount (00,01,01) = 2+1 = 3 displayObjectCount(object00.getInstanceCount()); //total InstanceCount (00,01,01) = 2+1 = 3 displayObjectCount(object01.getInstanceCountNonStatic()); //InstanceCountNonStatic (01) = 1 displayObjectCount(object00.getInstanceCountNonStatic()); //InstanceCountNonStatic (00) = 1 System.out.println("");

  33. object01 = new Counting(); //create 1 object 01 instance of the Class again. System.out.println("\tAt Point 03: After creating the third object01: " + "\n\tUse getInstanceCt method to get value of static field" + "\n\tUse getInstance method to get value of static field" + "\n\tand getInstanceCountNonStatic method to get non-static field"); displayObjectCount(Counting.getInstanceCt()); //total InstanceCount (00,01,01,01) = 3+1 = 4 displayObjectCount(object01.getInstanceCount()); //total InstanceCount (00,01,01,01) = 3+1 = 4 displayObjectCount(object00.getInstanceCount()); //total InstanceCount (00,01,01,01) = 3+1 = 4 displayObjectCount(object01.getInstanceCountNonStatic()); //InstanceCountNonStatic (01) = 1 displayObjectCount(object00.getInstanceCountNonStatic()); //InstanceCountNonStatic (00) = 1 System.out.println("");

  34. publicstaticvoid displayObjectCount(intnumberObjectCount) { System.out.println("\t" + numberObjectCount + " instances of the class was created."); }//end of displayObjectCount }//end of Chap08_Count_Objects Counting object02 = new Counting(); //Create 1 object 02 instance of the Class. System.out.println("\tAt Point 04: After creating an object02: " + "\n\tUse getInstanceCt method to get value of static field" + "\n\tUse getInstance method to get value of static field" + "\n\tand getInstanceCountNonStatic method to get non-static field"); displayObjectCount(Counting.getInstanceCt()); //total InstanceCount (00,01,01,01,02) = 4+1 = 5 displayObjectCount(object02.getInstanceCount()); //total InstanceCount (00,01,01,01,02) = 4+1 = 5 displayObjectCount(object01.getInstanceCount()); //total InstanceCount (00,01,01,01,02) = 4+1 = 5 displayObjectCount(object00.getInstanceCount()); //total InstanceCount (00,01,01,01,02) = 4+1 = 5 displayObjectCount(object02.getInstanceCountNonStatic()); //InstanceCountNonStatic (02) = 1 displayObjectCount(object01.getInstanceCountNonStatic()); //InstanceCountNonStatic (01) = 1 displayObjectCount(object00.getInstanceCountNonStatic()); //InstanceCountNonStatic (00) = 1 }//end of main

  35. publicstaticvoid displayObjectCount(intnumberObjectCount) { System.out.println("\t" + numberObjectCount + " instances of the class was created."); }//end of displayObjectCount }//end of Chap08_Count_Objects

  36. Counting object02 = new Counting();//Create 1 object 02 instance of the Class. System.out.println("\tAt Point 04: After creating an object02: " + "\n\tUse getInstanceCt method to get value of static field" + "\n\tUse getInstance method to get value of static field" + "\n\tand getInstanceCountNonStatic method to get non-static field"); displayObjectCount(Counting.getInstanceCt());//total InstanceCount (00,01,01,01,02) = 4+1 = 5 displayObjectCount(object02.getInstanceCount());//total InstanceCount (00,01,01,01,02) = 4+1 = 5 displayObjectCount(object01.getInstanceCount());//total InstanceCount (00,01,01,01,02) = 4+1 = 5 displayObjectCount(object00.getInstanceCount());//total InstanceCount (00,01,01,01,02) = 4+1 = 5 displayObjectCount(object02.getInstanceCountNonStatic());//InstanceCountNonStatic (02) = 1 displayObjectCount(object01.getInstanceCountNonStatic());//InstanceCountNonStatic (01) = 1 displayObjectCount(object00.getInstanceCountNonStatic());//InstanceCountNonStatic (00) = 1 }//end of main package Ch08_CountObjects_01; publicclass Counting { privatestaticintinstanceCount;//either way //private static int instanceCount = 0; //When a field instanceCount is declared with //the key word static there will be only one copy //of the field in memory and is shared by all //instances of the class. privateintinstanceCountNonStatic; //private int instanceCountNonStatic = 0; /** * The constructor increases the static field * instanceCount. This keeps track of the number * of instances of this class that are created. */ public Counting() { instanceCount++; instanceCountNonStatic++; } /** * The getInstanceCount method returns the * number of instances of this class that * have been created. * @return The value in the instanceCount field */ publicint getInstanceCount() { returninstanceCount; } publicstaticint getInstanceCt() { returninstanceCount; } publicint getInstanceCountNonStatic() { returninstanceCountNonStatic; } }

  37. Counting object02 = new Counting();//Create 1 object 02 instance of the Class. System.out.println("\tAt Point 04: After creating an object02: " + "\n\tUse getInstanceCt method to get value of static field" + "\n\tUse getInstance method to get value of static field" + "\n\tand getInstanceCountNonStatic method to get non-static field"); displayObjectCount(Counting.getInstanceCt());//total InstanceCount (00,01,01,01,02) = 4+1 = 5 displayObjectCount(object02.getInstanceCount());//total InstanceCount (00,01,01,01,02) = 4+1 = 5 displayObjectCount(object01.getInstanceCount());//total InstanceCount (00,01,01,01,02) = 4+1 = 5 displayObjectCount(object00.getInstanceCount());//total InstanceCount (00,01,01,01,02) = 4+1 = 5 displayObjectCount(object02.getInstanceCountNonStatic());//InstanceCountNonStatic (02) = 1 displayObjectCount(object01.getInstanceCountNonStatic());//InstanceCountNonStatic (01) = 1 displayObjectCount(object00.getInstanceCountNonStatic());//InstanceCountNonStatic (00) = 1 }//end of main package Ch08_CountObjects_01; publicclass Counting { privatestatic int instanceCount;//either way //private static int instanceCount = 0; //When a field instanceCount is declared with //the key word static there will be only one copy //of the field in memory and is shared by all //instances of the class. privateint instanceCountNonStatic; //private int instanceCountNonStatic = 0; /** * The constructor increases the static field * instanceCount. This keeps track of the number * of instances of this class that are created. */ public Counting() { instanceCount++; instanceCountNonStatic++; }

  38. Counting object02 = new Counting();//Create 1 object 02 instance of the Class. System.out.println("\tAt Point 04: After creating an object02: " + "\n\tUse getInstanceCt method to get value of static field" + "\n\tUse getInstance method to get value of static field" + "\n\tand getInstanceCountNonStatic method to get non-static field"); displayObjectCount(Counting.getInstanceCt());//total InstanceCount (00,01,01,01,02) = 4+1 = 5 displayObjectCount(object02.getInstanceCount());//total InstanceCount (00,01,01,01,02) = 4+1 = 5 displayObjectCount(object01.getInstanceCount());//total InstanceCount (00,01,01,01,02) = 4+1 = 5 displayObjectCount(object00.getInstanceCount());//total InstanceCount (00,01,01,01,02) = 4+1 = 5 displayObjectCount(object02.getInstanceCountNonStatic());//InstanceCountNonStatic (02) = 1 displayObjectCount(object01.getInstanceCountNonStatic());//InstanceCountNonStatic (01) = 1 displayObjectCount(object00.getInstanceCountNonStatic());//InstanceCountNonStatic (00) = 1 }//end of main /** * The getInstanceCount method returns the * number of instances of this class that * have been created. * @return The value in the instanceCount field */ publicint getInstanceCount() { returninstanceCount; } publicstaticint getInstanceCt() { returninstanceCount; } publicint getInstanceCountNonStatic() { returninstanceCountNonStatic; } }//end of Class Counting

  39. 0 instances of the class was created. 1 instances of the class was created. 1 instances of the class was created. 1 instances of the class was created. 2 instances of the class was created. At Point 01: After creating an object01: Use getInstanceCt method to get value of static field Use getInstance method to get value of static field and getInstanceCountNonStatic method to get non-static field 2 instances of the class was created. 2 instances of the class was created. 1 instances of the class was created. 1 instances of the class was created. At Point 02: After creating an object01 again: Use getInstanceCt method to get value of static field Use getInstance method to get value of static field and getInstanceCountNonStatic method to get non-static field 3 instances of the class was created. 3 instances of the class was created. 3 instances of the class was created. 1 instances of the class was created. 1 instances of the class was created.

  40. At Point 03: After creating the third object01: Use getInstanceCt method to get value of static field Use getInstance method to get value of static field and getInstanceCountNonStatic method to get non-static field 4 instances of the class was created. 4 instances of the class was created. 4 instances of the class was created. 1 instances of the class was created. 1 instances of the class was created. At Point 04: After creating an object02: Use getInstanceCt method to get value of static field Use getInstance method to get value of static field and getInstanceCountNonStatic method to get non-static field 5 instances of the class was created. 5 instances of the class was created. 5 instances of the class was created. 5 instances of the class was created. 1 instances of the class was created. 1 instances of the class was created. 1 instances of the class was created.

  41. Static Fields Class fields are declared using the static keyword between the access specifier and the field type. private static int instanceCount = 0; The field is initialized to 0 only once, regardless of the number of times the class is instantiated. Primitive static fields are initialized to 0 if no initialization is performed.private static int instanceCount;//it works Examples: Countable.java, StaticDemo.java This static field, instanceCount has a value 0 which is belonged to all created instances of a class.

  42. Static Fields instanceCount field (static) 3 Object1 Object2 Object3 This static method, getInstanceCount public static int getInstanceCount(){ return instanceCount; }

  43. Static Methods Methods can also be declared static by placing the static keyword between the access modifier and the return type of the method. public static double milesToKilometers(double miles) {…} When a class contains a static method, it is not necessary to create an instance of the class in order to use the method. double kilosPerMile = Metric.milesToKilometers(1.0); Examples: Metric.java, MetricDemo.java This static method, getInstanceCount get the current value of the static field instanceCount, which is belonged to all created instances of a class.

  44. Static Methods Static methods are convenient because they may be called at the class level. e.g., miles = Metric.kilometersToMiles(kilos); They are typically used to create utility classes, such as the Math class in the Java Standard Library. e.g., ans = Math.sqrt(5.25); Static methods may not communicate with instance fields, but only static fields.

  45. When a class contains a static method, it is not necessary to create an instance of the class in order to use the method. publicstaticvoid main(String[] args) { intobjectCount; intobjectCountNonStatic; //Create four instances of the Countable class Counting object00; displayObjectCount(Counting.getInstanceCt()); object00 = new Counting(); displayObjectCount(object00.getInstanceCount()); Counting object01 = new Counting(); displayObjectCount(Counting.getInstanceCt()); objectCount = object01.getInstanceCount(); System.out.println("\nAt Point 01: object01: " + "Use static getInstance method"); displayObjectCount(objectCount);

  46. publicclass Counting { privatestaticintinstanceCount;//either way //private static int instanceCount = 0; privateintinstanceCountNonStatic;//either way //private int instanceCountNonStatic = 0; public Counting() //Constructor { instanceCount++; instanceCountNonStatic++; } publicint getInstanceCount() { returninstanceCount; } publicstaticint getInstanceCt() //static method { returninstanceCount; } }

  47. The output is: 0 instances of the class was created. 1 instances of the class was created. 2 instances of the class was created. At Point 01: object01: Use static getInstance method 2 instances of the class was created.

More Related