Lecture7

Lecture7 - Chapter 10 Numbers and Statics Static method and...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Chapter 10 Numbers and Statics Static method and Variables Wrapper Class Two types of methods. Instance methods: associated with an object and use the instance variables of that object. Static methods (also called class methods ): use no instance variables of any object of the class they are defined in. public static double mean(int[] p) Static methods can be invoked through the class name. Why declare a method static Documentation . Anyone seeing that a method is static will know how to call it. Efficiency . A compiler will usually produce slightly more efficient code because no implicit object parameter has to be passed to the method.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Math class: The methods of the Math class are static methods . Math.random() Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. double rl = Math.random() ; int r2 = (int) (Math.random() * 5); Math.abs() int x = Math.abs(-240); // returns 240 double d = Math.abs(240.45); // returns 240.45 Math.min() Returns a value that is the minimum of the two arguments. int x = Math.min(24, 240); // returns 24 double y = Math.min(90876.5, 90876.49) ; // returns 90876.49
Background image of page 2
3 Calling static methods class MyUtils { . . . public static double mean(int[] p) { int sum = 0; // sum of all the //elements for (int i=0; i<p.length; i++) { sum += p[i]; } return ((double)sum)/p.length; } . . . } Called from within the same class Just write the static method name. Eg, // Called from inside the MyUtils class double avgAtt = mean(attendance); Called from outside the class double avgAtt = MyUtils. mean(attendance);
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Static and non-static variables When a number of objects are created from the same class blueprint, they each have their own distinct copies of instance variables . In the case of the Bicycle class, the instance variables are cadence , gear , and speed . Each Bicycle object has its own values for these variables, stored in different memory locations. Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables . They are associated with the class, rather than with any object. Every object shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable. Static variable: value is the same for all objects of the class
Background image of page 4
5 Suppose you wanted to count how many Duck objects are being created while your program is running. public class Duck { private int size; private int duckCount = 0; public Duck() { duckCount++; } public void setSize(int s) { size = s; } public int getSize() { return size; } } public class Duck { private int size; private static int duckCount = 0; public Duck () { duckCount++; } public void setSize(int s) { size = s; } public int getSize() { return size; } }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Static variables can be invoked through the class name. class Cat {
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/12/2010 for the course COE 318 taught by Professor Ken during the Spring '08 term at Ryerson.

Page1 / 32

Lecture7 - Chapter 10 Numbers and Statics Static method and...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online