CSCI 1302 Guided Notes: Chapter 9 Static Fields and ... ?· CSCI 1302 Guided Notes: Chapter 9 – Static…

Download CSCI 1302 Guided Notes: Chapter 9 Static Fields and ... ?· CSCI 1302 Guided Notes: Chapter 9 – Static…

Post on 28-Aug-2018

213 views

Category:

Documents

1 download

Embed Size (px)

TRANSCRIPT

<ul><li><p>CSCI 1302 Guided Notes: Chapter 9 Static Fields and Methods Dr. Phelps </p><p>Static Fields and Methods (Section 9.1) </p><p>1. What is the difference between an instance field and a static field? </p><p>2. What are 3 uses of static fields? </p><p>3. What action is possible with a static method that isnt possible with an instance method? </p><p>4. Describe the limitation of static methods? </p></li><li><p>CSCI 1302 Guided Notes: Chapter 9 Static Fields and Methods Dr. Phelps </p><p>Activity: Static Fields and Methods (StaticRectangle) </p><p>(Files: Rectangle.java and RectangleDemo.java) </p><p> 1 /** The Rectangle class defines static numberOfRec and symbol fields**/ </p><p> 2 </p><p> 3 public class Rectangle </p><p> 4 { </p><p> 5 private int height, width; </p><p> 6 private static int numberOfRec = 0; </p><p> 7 private static char symbol = '*'; </p><p> 8 </p><p> 9 //Constructor </p><p>10 public Rectangle (int hig, int wid) </p><p>11 { height = hig; </p><p>12 width = wid; </p><p>13 numberOfRec++; //updating static field </p><p>14 } </p><p>15 </p><p>16 //Draws rectangle on screen </p><p>17 public void draw() </p><p>18 { </p><p>19 for (int row =1; row</p></li><li><p>CSCI 1302 Guided Notes: Chapter 9 Static Fields and Methods Dr. Phelps </p><p> 1 public class RectangleDemo </p><p> 2 { </p><p> 3 public static void main(String [] args) </p><p> 4 { </p><p> 5 //Declare rectA </p><p> 6 Rectangle rectA = new Rectangle(4, 2); </p><p> 7 rectA.display(); </p><p> 8 </p><p> 9 //Declare rectB </p><p>10 Rectangle rectB = new Rectangle(2, 6); </p><p>11 rectB.display(); </p><p>12 </p><p>13 //Draw Rectangles </p><p>14 rectA.setSymbol('$'); </p><p>15 rectA.draw(); </p><p>16 rectB.draw(); </p><p>17 System.out.println(); </p><p>18 Rectangle.setSymbol('%'); </p><p>19 </p><p>20 //Display fields for both Rectangles </p><p>21 rectA.display(); </p><p>22 rectB.display(); </p><p>23 } </p><p>24 } </p><p>1. numberOfRec is a static field thus is shared by all Rectangle objects -- there is only one numberOfRec variable for the </p><p>Rectangle class. In contrast, each object has its own copy of height and width instance fields. What is the output of lines 7 </p><p>and 11? </p><p>a) rectA.display() </p><p>height: </p><p>width: </p><p>numberOfRec: </p><p>symbol: </p><p>b) rectB.display(); </p><p>height: </p><p>width: </p><p>numberOfRec: </p><p>symbol: </p><p>2. When a class contains a static method, it is NOT necessary to create an instance of the class in order to use the </p><p>method. Notice the setSymbol method may called two different ways: </p><p>rectA.setSymbol('*'); ); // Line 14: (*discouraged because it does not make clear it is static*) </p><p>Rectangle.setSymbol('%'); //Line 18: Uses the class name to access static method </p><p>a) What symbol will be used to draw the rectangles on lines 15 and 16? Draw both rectangles. </p><p>b) What is the output of lines 21 and 22? </p><p> rectA.display() </p><p>height: </p><p>width: </p><p>numberOfRec: </p><p>symbol: </p><p> rectB.display(); </p><p>height: </p><p>width: </p><p>numberOfRec: </p><p>symbol </p><p>rectB </p><p>rectA </p><p>Rectangle static fields </p><p>symbol </p><p>numberOfRec </p><p>width </p><p>width height </p><p>height </p></li></ul>