CH05 - Chapter 5 Java: an Introduction to Computer Science...

Info iconThis preview shows pages 1–9. 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 DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Chapter 5 Java: an Introduction to Computer Science & Programming - Walter Savitch Chapter 5 Programming with Methods Static Methods and Static Variables Designing Methods Polymorphism Constructors Information Hiding Revisited Packages More About Objects and Methods Chapter 5 Java: an Introduction to Computer Science & Programming - Walter Savitch The " this . " Parameter this. refers to the object that contains the reference Methods called in an object definition file do not need to reference itself You may either use " this.", or omit it, since it is presumed For example, if answerOne() is a method defined in the class Oracle : public class Oracle { ... //One way to invoke the answerOne method defined //in this file: this.answerOne(); //Another way is to omit "this." answerOne(); //"this." is presumed ... } Chapter 5 Java: an Introduction to Computer Science & Programming - Walter Savitch 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(); ... Chapter 5 Java: an Introduction to Computer Science & Programming - Walter Savitch 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 Chapter 5 Java: an Introduction to Computer Science & Programming - Walter Savitch Static Methods Some class methods don't not need an object to do their job Must declare the method as static : // Method inside class CircleFirstTry public static double area(double radius) { return (PI*radius*radius); } Use class name instead of an object name to invoke them CircleFirstTry.area(myRadius); Also called class methods Chapter 5 Java: an Introduction to Computer Science & Programming - Walter Savitch Static Methods (cont.) Since a static method does not need a calling object: Cannot refer to ( non-static) instance variable of the class. Cannot call a non-static method of the class unless creates object of the class as calling object Chapter 5 Java: an Introduction to Computer Science & Programming - Walter Savitch Lets look at some programs In chapter 5: CircleFirstTry & CircleDemo PlayCircle & PlayCircleDemo Chapter 5 Java: an Introduction to Computer Science & Programming - Walter Savitch Uses for Static Methods Provide libraries of useful and related methods.of useful and related methods....
View Full Document

Page1 / 37

CH05 - Chapter 5 Java: an Introduction to Computer Science...

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

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