1 / 12

CS110 Lecture 9 February 24, 2004

CS110 Lecture 9 February 24, 2004. Announcements hw3 due Thursday February 26 exam Tuesday March 2 Agenda questions scope classes vs objects how parameters really work. Scope. scope of a variable or method: where its unadorned name is visible to the program

dillan
Download Presentation

CS110 Lecture 9 February 24, 2004

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS110 Lecture 9February 24, 2004 • Announcements • hw3 due Thursday February 26 • exam Tuesday March 2 • Agenda • questions • scope • classes vs objects • how parameters really work Lecture 9

  2. Scope • scope of a variable or method: where its unadorned name is visible to the program • Usually: the block { … } where it’s declared • Examples from HLine.java • scope of all fields: lines 15-114 • scope of screen (line 100) : lines 100-113 • scope of length (line 25): lines 25-29 • scope of i (line 41): lines 41-43 • Scope of a method is the class it’s declared in • public declaration does not change scope Lecture 9

  3. Scope • To see a method or field outside its scope, qualify the name of the method or field: • account.getBalance() • System.out.println() • this.contents • Compare 7-6440, 287-6440 and (617)287-6440 • But • account.balance will fail because balance is private (an unlisted phone number) Lecture 9

  4. import • Import statement qualifies scope for class names • No need to import String, … • java packages • import java.lang.Date • import …BigInteger • … you could write your own BigInteger class Lecture 9

  5. static • Java keyword for belongs to whole class rather than to an instance of the class • Static things are rare, objects are common: too much static is bad design • public static void main( ) • main() is a static method - it can run before any objects are created with new • TestShapes (like many testing programs) is all static: there is a TestShapes class, but never a TestShapes object (although main uses objects Lecture 9

  6. What can main() see? • HLine is meant to be a client class • private fields • public getters, setters, other methods as appropriate • HLine has a static main method, for unit testing • main in HLine • can’t refer to length field or paintOn method, since those belong only to HLine objects • can instantiate an HLine object, and then send it a paintOn message Lecture 9

  7. Static tools in the library • To invoke a static method, send a message to the class (there is no object) - syntax ClassName.methodName( args ) • Math.sqrt( x ) • Calendar.getInstance() a factory method -Java designers chose this rather than new Calendar • UnitTest.javaline 21: HLine.main(args) sends message to HLine class to run main() there Lecture 9

  8. static fields • Syntax for accessing static field: ClassName.fieldName (e.g. System.out ) (no System constructor, no System object) • Like global variables (in other languages) • In Integer class (part of Java API) public static final int MAX_VALUE= 2147483647; • final: Java keyword for “can’t be changed” int big = Integer.MAX_VALUE; // OK Integer.MAX_VALUE = 255; // error • Naming convention for final fields: ALL_CAPS Lecture 9

  9. How parameters really work • Box.java line 143 sends a message: box2.paintOn( screen, 2, 2 ); • Execution shifts to method at Box.java line 52: public void paintOn( Screen s, int x, int y) • Value of parameter • sin method is value of screenin message • xin method is (first) 2in message • yin method is (second) 2in message Lecture 9

  10. How parameters really work • Name of parameter (s) in method declaration need not match the name of the value in the message (screen) • You can’t even think they should match: • The value in the message might not even have a name (the 2 in the example) • The method can be written before the client (in some other class) has even been imagined - and the client programmer does not have access to the source code with the method declaration • The type of the value in the message must match the type in the method declaration Lecture 9

  11. In Box main Screen Screen screen: Box Box box1: Box Box int box2: width: 3 int width: 4 char pntCh: 'G' Lecture 9

  12. In Box main In Box paintOn Screen Screen screen: Box Box box1: Box Box Box int this: box2: width: 3 Screen out of scope int s: width: 4 int char 2 x: pntCh: 'G' int 2 y: Lecture 9

More Related