Announcements Homework P 1 is due today Homework





![Example static public void main (String args[ ]) { Employee v 1= new Employee(“Millett”); Example static public void main (String args[ ]) { Employee v 1= new Employee(“Millett”);](https://slidetodoc.com/presentation_image/069ca5f20e625f138f10434e5ddb3d91/image-6.jpg)


















- Slides: 24
Announcements • Homework P 1 is due today • Homework P 2 is due next Thursday • Your quizzes will be returned with P 1 CS 100 Lecture 8 1
Today’s Topics • Review • Static Fields and Methods • Conditional Operator CS 100 Lecture 8 2
Review • Detailed explanation of method calls yesterday • What were the four steps that occur when a method is invoked? • What’s x++ mean? • x++ vs. x-- CS 100 Lecture 8 3
Why Static? • Suppose that we would like to maintain, somehow, the number of instances of Employee that were ever created. The following does not work! public class Employee { public int no. Emps= 0; //Number of Employees ever created public String name; // Constructor -- An Employee named n public Employee(String n) { no. Emps= no. Emps+1; name= n; } } CS 100 Lecture 8 4
Use “static” Using the prefix qualifier static means that there is only ONE field no. Emps for the whole class, not one for each instance of the class: public class Employee { static public int no. Emps= 0; //Number Employees ever created public String name; //Employee’s name // Constructor -- An Employee named n public Employee(String n) { no. Emps= no. Emps+1; name= n; } // Return no. of Employees ever created public int get. No. Emps() {return no. Emps; }} CS 100 Lecture 8 5
Example static public void main (String args[ ]) { Employee v 1= new Employee(“Millett”); System. out. println("No. of emps" + v 1. no. Emps); Employee v 2= new Employee(“Ubik”); System. out. println("No. of emps" + v 2. no. Emps); } CS 100 Lecture 8 6
Picture of Previous Class Employee --------------------------no. Emps _______ Name Millett Name Ubik Frame for main v 1 ____ CS 100 v 2 ____ Lecture 8 7
How this works • A static field (or method) is called a class variable (or method). • At runtime, a box for a class named C contains all the class variables and methods. Whenever 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 executed, then in the surrounding box, etc. CS 100 Lecture 8 8
In Practice. . . • You can (should) use the name of the class, instead of a class variable, to refer-ence a static field or method Employee v 1= new Employee(“Millett”); Employe v 2= new Employee(“Ubik”); • Instead of writing System. out. println(v 1. no. Emps); write System. out. println(Employee. no. Emps); CS 100 Lecture 8 9
Private and Static 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 no. Emps= 0; //No. Empls ever created public String name; //Employee’s name // Constructor -- An Employee named n public Employee(String n) { no. Emps= no. Emps+1; name= n; } // Return no. of Employees ever created public int get. No. Emps() {return no. Emps; } } CS 100 Lecture 8 10
Final and Static • Constants declared using modifier final also often declared using static. • Why? • The value of constants can’t be changed, so there might as well be only one value across all objects of the class • Constants cannot be declared inside a method. CS 100 Lecture 8 11
Review so far. . . • Local variables -- local to a particular method – cannot be declared static • Instance variables -- used in a particular object • Class variables -- shared among all instances of a class – changing the value here changes it for all instances of that class CS 100 Lecture 8 12
Static Methods • Referred to as a class method • Don’t have to declare and instantiate a particular object in order to invoke a class/static method • Static methods cannot reference instance variables -- only local variables, static fields and parameters CS 100 Lecture 8 13
So, why is main static? • main needs to be executable by the interpreter without instantiating an object from the class containing main • Many other static methods can be found in Java API CS 100 Lecture 8 14
Example of static method // Return the number of Employees ever created static public int get. No. Emps() {return no. Emps; } // A static method may be called only using the class name. // It may not be called using a variable name. Employee v 1= new Employee(“Millett”); // WRONG: System. out. println(v 1. get. No. Emps()); // RIGHT: System. out. println(Employee. get. No. Emps()); CS 100 Lecture 8 15
Math class - lots of static methods public final class Math { public static final double E = 2. 718284590452354; public static final double PI = 3. 14159265358979323846; 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. 5 f); } public static int max(int a, int b) {return (a >= b) ? a : b; } CS 100 Lecture 8 16
How to call these methods? • Recall: Use class name not object name to call static methods: • double x = Math. sin(183. 2); • double root = Math. sqrt(33); • int y = Math. max(5, 4); CS 100 Lecture 8 17
Where to find this stuff? • In folder: Code. Warrior / Java Support / java / lang • Poke around, don’t change anything. • One good place for Java docs: http: //java. sun. com CS 100 Lecture 8 18
Conditional Operator • We call this a ternary operator (requires 3 operands) +, -, etc. are binary operators • Syntax: condition ? expression 1 : expression 2; • Sort of an abbreviated if-else statement • Use it sparingly. • expression 1 and expression 2 must evaluate to the same type CS 100 Lecture 8 19
Example of conditional operator total = (total > MAX) ? total + 1 : total + 2; is equivalent to: if (total > MAX) total = total + 1; else total = total + 2; CS 100 Lecture 8 20
Another example of ternary op int x; int y; x = Integer. parse. Int(stdin. read. Line()); y = Integer. parse. Int(stdin. read. Line()); System. out. println(“The largest of your inputs is ” + ((x >=y) ? x : y) ); CS 100 Lecture 8 21
Floats and Doubles • float -- 32 bits, double -- 64 bits • floats have only 7 significant digits, therefore, even though something like 49786. 2123 is in range, you need to use a double • Java assumes all floating point literals (e. g. 34. 65) are doubles. If you want a float, append ‘f’ as in 34. 65 f CS 100 Lecture 8 22
Precision in Java wrt floating point values • Do not use == to compare floating point values • Better way: if (Math. abs(f 1 - f 2) < TOLERANCE) System. out. println(“Pretty much equal. ”); CS 100 Lecture 8 23
Wrapper classes • Each primitive has a wrapper class (already seen Integer and Double) • Available in java. lang package. • Objects created from these class can be useful for converting value of one type into another • Integer. parse. Int(), Double. value. Of() are both static methods, for example • Check out Appendix O in your text CS 100 Lecture 8 24
























