10/4/2006 CMSC 131 Fall 2006Rance Cleaveland©2006 Univeristy of MarylandLecture 15:Intra-Class Method CallingLast time:1. Project #3 due 10/92. Exam #1 coming 10/113. APIs, comments and documentationToday:1. Project #3 due 10/92. Exam #1 coming 10/113. APIs, comments and documentationCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland1Project #3 Is Assigned! The assignment is on the CMSC 131 web-site (click “Projects” link). It is due Monday, 10/9 at 11 pm The project is open Start now! Read entire assignment from beginning to end before starting to code Check out assignment now from CVS Follow the instructions exactly, as much of grading is automatedCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland2Exam #1 Is 10/11 in Discussion SectionTake the test in your own section Test will cover material since beginning of semester Test will be closed book, closed neighbor Study!CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland3Study When!?! Project and test are very close to one another This is part of college What to do? Start project immediately Aim to finish project by Friday Start studying for exam on Saturday even if project not yet finished You can do it!!CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland4Recall: Beginning with Project #3, you will be graded on comments as well as indentation Comment: Every variable (what is variable for?) Every method (what does method do?) Every class (what is class for? who wrote it?)CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland5Methods Calling Methods Four kinds of methods so far public non-static public static private non-static private static We have seen that methods can call methods Example: Date.print() / Date.println() methods from Lecture #10 Date.print() printed date information Date.println() printed same info, followed by carriage return The second called the first to ensure uniformity of date format Question: What kind of methods can call other kinds of methods?CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland6Examples Assume following class specificationpublic class C {public void f () { …}public static void g () {…}private void h () {…}private static void i () {…}} Can: Body of f call g?Yes Body of g call f?No Body of h call g?Yes Body of f call h?Yes Body of h call f?Yes Body of i call g?Yes Body of i call h?NoCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland7What Is Rule? Recall: Static methods are owned by class Other methods are owned by objects So for methods in same class: Non-static methods may call static or non-static methods Static methods may call other static methods Public / private doesn’t matter in relation to who may call whomCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland8When Should One Method Call Another?Whenever it call! Want to avoid duplication of effort Want to eliminate possibilities for inconsistent behavior Example (Date.java for this class meeting):// The method for converting dates to stringspublic String toString () {return (month + separator + day + separator + year);}// The method for printing dates to System.outpublic void print () {System.out.print (toString ());}// Method for printing dates followed by carriage returnpublic void println () {print ();System.out.println ();}CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland9Invoking Static Methods May be done … Through class: C.g (); Through object:C obj = new C ();obj.g(); Rule of thumb: always invoke through class! Reinforces static status of class Avoids confusion between instance, static methodsCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland10Accessing static Variables inside MethodsRules are similar to method invocation! Static methods may refer only to local or static variables in body Non-static methods may refer to local, static, instance variables in body Public / private doesn’t matter in this context Examplepublic class C {public int a = 0;public static int b = 1;public void f () { …}public static void g () {…}} Can body of f refer to a?Yes Can body of f refer to b?Yes Can body of g refer to a?No Can body of g refer to b?YesCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland11Course Review So Far Hardware? Software? CPU? Main memory? Bit / byte / word? Memory address? Operating system? Application? Machine / assembly languages? Compilers / interpreters? Java Virtual Machine? Primitive / built-in types? String? Expressions? Control flow? If / else / else if? Blocks? Scanner? Logical operators? Short-circuiting? Loops? final? Dangling else? Type casting / upcasting / downcasting? Software lifecycle? Objects? Classes? Heap? Stack? Reference-typed variable? Pseudo-code? Object equality? Aliasing? Constructors? Overriding? Static / public / private? Stack frame? Library?
View Full Document