Transcript
Page 1: CSCI 1302 Guided Notes: Chapter 9 Static Fields and ... · CSCI 1302 Guided Notes: Chapter 9 – Static Fields and Methods Dr. Phelps Static Fields and Methods (Section 9.1) 1. What

CSCI 1302 Guided Notes: Chapter 9 – Static Fields and Methods Dr. Phelps

Static Fields and Methods (Section 9.1)

1. What is the difference between an instance field and a static field?

2. What are 3 uses of static fields?

3. What action is possible with a static method that isn’t possible with an instance method?

4. Describe the limitation of static methods?

Page 2: CSCI 1302 Guided Notes: Chapter 9 Static Fields and ... · CSCI 1302 Guided Notes: Chapter 9 – Static Fields and Methods Dr. Phelps Static Fields and Methods (Section 9.1) 1. What

CSCI 1302 Guided Notes: Chapter 9 – Static Fields and Methods Dr. Phelps

Activity: Static Fields and Methods (StaticRectangle)

(Files: Rectangle.java and RectangleDemo.java)

1 /** The Rectangle class defines static numberOfRec and symbol fields**/

2

3 public class Rectangle

4 {

5 private int height, width;

6 private static int numberOfRec = 0;

7 private static char symbol = '*';

8

9 //Constructor

10 public Rectangle (int hig, int wid)

11 { height = hig;

12 width = wid;

13 numberOfRec++; //updating static field

14 }

15

16 //Draws rectangle on screen

17 public void draw()

18 {

19 for (int row =1; row<=height; row++)

20 {

21 for (int col=1; col<=width;col++)

22 System.out.print(symbol); //displays symbol

23

24 System.out.println();

25 }

26 System.out.println();

27 }

28

29 //This method is used to print the values of all fields

30 public void display()

31 {

32 System.out.println("Height: " + height);

33 System.out.println("Width: " + width);

34 System.out.println("numberOfRec: " + numberOfRec);

35 System.out.println("symbol: " + symbol);

36 System.out.println();

37 }

38

39 //Change the symbol used when drawing rectangles

40 public static void setSymbol(char sym)

41 {

42 symbol = sym;

43 }

44 }

Page 3: CSCI 1302 Guided Notes: Chapter 9 Static Fields and ... · CSCI 1302 Guided Notes: Chapter 9 – Static Fields and Methods Dr. Phelps Static Fields and Methods (Section 9.1) 1. What

CSCI 1302 Guided Notes: Chapter 9 – Static Fields and Methods Dr. Phelps

1 public class RectangleDemo

2 {

3 public static void main(String [] args)

4 {

5 //Declare rectA

6 Rectangle rectA = new Rectangle(4, 2);

7 rectA.display();

8

9 //Declare rectB

10 Rectangle rectB = new Rectangle(2, 6);

11 rectB.display();

12

13 //Draw Rectangles

14 rectA.setSymbol('$');

15 rectA.draw();

16 rectB.draw();

17 System.out.println();

18 Rectangle.setSymbol('%');

19

20 //Display fields for both Rectangles

21 rectA.display();

22 rectB.display();

23 }

24 }

1. numberOfRec is a static field thus is shared by all Rectangle objects -- there is only one numberOfRec variable for the

Rectangle class. In contrast, each object has its own copy of height and width instance fields. What is the output of lines 7

and 11?

a) rectA.display()

height:

width:

numberOfRec:

symbol:

b) rectB.display();

height:

width:

numberOfRec:

symbol:

2. When a class contains a static method, it is NOT necessary to create an instance of the class in order to use the

method. Notice the setSymbol method may called two different ways:

rectA.setSymbol('*'); ); // Line 14: (*discouraged because it does not make clear it is static*)

Rectangle.setSymbol('%'); //Line 18: Uses the class name to access static method

a) What symbol will be used to draw the rectangles on lines 15 and 16? Draw both rectangles.

b) What is the output of lines 21 and 22?

rectA.display()

height:

width:

numberOfRec:

symbol:

rectB.display();

height:

width:

numberOfRec:

symbol

rectB

rectA

Rectangle static fields

symbol

numberOfRec

width

width height

height


Top Related