ch05 - More About Objects and Methods Chapter 5 When an...

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: More About Objects and Methods Chapter 5 When an Object Is Required • Methods called outside the object definition require an object to precede the method name • For example: Oracle myOracle = new Oracle(); //myOracle is not part of the definition code //for Oracle ... //dialog is a method defined in Oracle class myOracle.dialog(); ... The " this . " Parameter • this refers to the calling object of the method • Methods called in an class definition file do not need to reference itself • You may either use " this. ", or omit it • For example, if answerOne() is a method defined in the class Oracle : public class Oracle { … myMethod(…) { //invoke the answerOne method defined this.answerOne(); answerOne(); //"this" is the default object ... } } null • If the compiler requires you to initialize a class variable, you can set it to null if you have no other initial value. • You can use == and != to see if a class variable is equal to null , because null is used like an address. Gotcha : Null Pointer Exception • If you invoke a method using a variable that is initialized to null, you will get an error message that says "Null Pointer Exception". Species specialSpecies = null; specialSpecies.readInput(); Species specialSpecies = new Species(); specialSpecies.readInput(); Null Pointer Exception OK Static Methods • Some methods don't need an object to do their job – For example, methods to calculate logarithm: just pass the required parameters and return the logarithm • Use the class name instead of an object name to invoke them • Also called class methods • Static methods are associated with a class —the method behavior is “static” • Nonstatic methods are associated with an object —the method behavior depends on the object and hence “nonstatic” Uses for Static Methods • main method—the starting point of a program • Static methods are commonly used to provide libraries of useful and related methods. Examples: – SavitchIn defines methods for keyboard input • not provided with Java • no need to create a SavitchIn object • methods include readLineInt , readLineDouble , etc. • see the appendix – the Math class • provided with Java • no need to create a Math object • methods include pow , sqrt , max , min , etc. • more details next The Math Class (p335 4 th Ed.) • Includes constants Math.PI (approximately 3.14159) and Math.E (base of natural logarithms which is approximately 2.72) • Includes three similar static methods: round , floor , and ceil – All three return whole numbers (although they are type double ) – Math.round returns the whole number nearest its argument Math.round(3.3) returns 3.0 and Math.round(3.7) returns 4.0 – Math.floor returns the nearest whole number that is equal to or less than its argument Math.floor(3.3) returns 3.0 and Math.floor(3.7) returns 3.0 – Math.ceil (short for ceiling) returns the nearest whole number that is equal to or greater than its argument Math.ceil(3.3) returns 4.0 and Math.ceil(3.7) returns 4.0 Static Methods...
View Full Document

{[ snackBarMessage ]}

Page1 / 51

ch05 - More About Objects and Methods Chapter 5 When an...

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

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