Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS100A, Lecture 7, 22 Sept Static fields and methods

Similar presentations


Presentation on theme: "CS100A, Lecture 7, 22 Sept Static fields and methods"— Presentation transcript:

1 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 //ever created public String name; // Constructor -- An Employee named n public Employee(String n) { noEmps= noEmps+1; name= n; } CS100A, Lect. 7, 22 Sept Static Fields and Methods

2 CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods
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: public class Employee { static public int 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 public int getNoEmps() {return noEmps;} CS100A, Lect. 7, 22 Sept Static Fields and Methods

3 CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods
static public void 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 Static Fields and Methods

4 CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods
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 Static Fields and Methods

5 CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods
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 Static Fields and Methods

6 CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods
Make the static field private, so it can’t be changed from outside the class. Have a “get” method to read its value. public class Employee { static private int 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 public int getNoEmps() {return noEmps;} CS100A, Lect. 7, 22 Sept Static Fields and Methods

7 CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods
Method Color contains static fields (and methods) public class Color implements java.io.Serializable { public final static Color red = new Color(255, 0, 0); public final static Color white = new Color(255, 255, 255); public final static Color yellow = new Color(255, 255, 0); public final static 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 Static Fields and Methods

8 CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods
A variable (or field) declared with prefix qualifier final may be assigned only once. Do it in the declaration. OKAY: final int x; x= 5; BETTER: final int x= 5; CS100A, Lect. 7, 22 Sept 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 static public int 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 Static Fields and Methods

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

11 CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods
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 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 Static Fields and Methods


Download ppt "CS100A, Lecture 7, 22 Sept Static fields and methods"

Similar presentations


Ads by Google