1 / 11

CS100A, Lecture 7, 22 Sept. 1998 Static fields and methods

CS100A, Lecture 7, 22 Sept. 1998 Static fields and methods. Suppose that we would like to main-tain, somehow, the number of instances of Employee that were ever created. The following does not work!. public class Employee { public int noEmps= 0; //No. of Emps

cathal
Télécharger la présentation

CS100A, Lecture 7, 22 Sept. 1998 Static fields and methods

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. CS100A, Lecture 7, 22 Sept. 1998Static fields and methods Suppose that we would like to main-tain, somehow, the number of instances of Employee that were ever created. The following does not work! . publicclass Employee { publicint noEmps= 0; //No. of Emps //ever created public String name; // Constructor -- An Employee named n public Employee(String n) { noEmps= noEmps+1; name= n; } } CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods

  2. Using the prefix qualifier static means that there is only ONE field noEmps for the whole class, not one for each instance of the class: publicclass Employee { staticpublicint noEmps= 0; //No. Empls //ever created public String name; //Employee’s name // Constructor -- An Employee named n public Employee(String n) { noEmps= noEmps+1; name= n; } // Return no. of Employees ever created publicint getNoEmps() {return noEmps;} } CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods

  3. staticpublicvoid main (String args[ ]) { Employee v1= new Employee(); System.out.println("No. of emps" + v1.noEmps); Employee v2= new Employee(); System.out.println("No. of emps" + v2.noEmps); } Class Employee ----------------------------------------------------- noEmps _______ Name Gries Name Cardie Frame for main v1 ____ v2 ____ CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods

  4. Model of Execution A static field (or method) is called a class variable (or method). At runtime, a box for a class named C con-tains all the class variables and methods. When-ever an instance of class C is created, it is placed in this box. A parameter or local variable declared as C v; is placed in the frame for the method in which it appears. This model assures that the rule we gave for finding a variable, when it is referenced, works: look first in the frame for method being execut-ed, then in the surrounding box, etc. CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods

  5. You can (should) use the name of the class, instead of a class variable, to refer-ence a static field or method Employee v1= new Employee(“Gries”); Employe v2= new Employee(“Cardie”); Instead of writing System.out.println(v1.noEmps); write System.out.println (Employee.noEmps); CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods

  6. Make the static field private, so it can’t be changed from outside the class. Have a “get” method to read its value. publicclass Employee { staticprivateint noEmps= 0; //No. Empls //ever created public String name; //Employee’s name // Constructor -- An Employee named n public Employee(String n) { noEmps= noEmps+1; name= n; } // Return no. of Employees ever created publicint getNoEmps() {return noEmps;} } CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods

  7. Method Color contains static fields (and methods) publicclass Color implements java.io.Serializable { publicfinalstatic Color red = new Color(255, 0, 0); publicfinalstatic Color white = new Color(255, 255, 255); publicfinalstatic Color yellow = new Color(255, 255, 0); publicfinalstatic Color pink = new Color(255, 175, 175); (The three arguments are RGB (red-green-blue) values) public String toString() { return getClass().getName() + "[r=" + getRed() + ",g=" + getGreen() + ",b=" + getBlue() + "]"; } CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods

  8. A variable (or field) declared with prefix qualifier final may be assigned only once. Do it in the declaration. OKAY: finalint x; x= 5; BETTER: finalint x= 5; CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods

  9. Methods, as well as fields, may be static A static method may refer only to parameters, local variables, and static fields. It may not refer to non-static fields of the class. In which the method appears. // Return the number of Employees ever created staticpublicint getNoEmps() {return noEmps;} A static method may be called only using the class name. It may not be called using a variable name. Employee v1= new Employee(“Gries”); WRONG: System.out.println(v1.getNoEmps()); RIGHT: System.out.println(Employee.getNoEmps()); CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods

  10. publicfinalclass Math { publicstaticfinaldouble E = 2.7182818284590452354; publicstaticfinaldouble PI = 3.14159265358979323846; publicstaticnativedouble sin(double a); publicstaticnativedouble cos(double a); publicstaticint round(float a) { return (int)floor(a + 0.5f); } publicstaticint max(int a, int b) { return (a >= b) ? a : b; } Class Math contains ONLY static fields and methods CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods

  11. On the PC, class Color (in file Color.java), and lots of other methods of the “abstract window toolkit”, should be in directory Program Files / Metrowerks / CodeWarrior / Java Support / Java Source / java / awt Class Math is in file Math.java in directory Program Files / Metrowerks / CodeWarrior / Java Support / Java Source / java / lang On the Mac, it will be in a similar place. DON’T CHANGE THESE FILES. But look at them. In Visual J++, highlight a class name and push button F1 to obtain a specification of the class. CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods

More Related