Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Second Look at Classes and Objects - 1 Static Class Members

Similar presentations


Presentation on theme: "A Second Look at Classes and Objects - 1 Static Class Members"— Presentation transcript:

1 A Second Look at Classes and Objects - 1 Static Class Members
Chapter 9 A Second Look at Classes and Objects - 1 Static Class Members

2 Contents A Quick Review of Instance Fields and Instance Methods
Static Class Members Static Fields Static Methods

3 I. A Quick Review of Instance Fields and Instance Methods
Each instance of a class has its own set of fields, which are known as instance fields. We can create several instances of a class and store different values in each instance's fields. Classes may have instance methods as well. When we call an instance method, it performs an operation on a specific instance of the class.

4 I. A Quick Review of Instance Fields and Instance Methods
Class Rectangle Rectangle - length - width + setLength() + setWidth() + getLength() + getWidth() + getArea()

5 Rectangle box = new Rectangle();
box.setLength(10.0); box.setWidth(20.0); A Rectangle object The box variable holds the address of a Rectangle bject. length width 0.0 address 0.0 A Rectangle object The box variable holds the address of a Rectangle bject. length width 10.0 address 0.0 A Rectangle object The box variable holds the address of a Rectangle bject. length width 10.0 address 20.0

6 Rectangle kitchen = new Rectangle(); Rectangle bedroom = new Rectangle();
The kitchen variable holds the address of a Rectangle bject. length width 0.0 A Rectangle object address 0.0 The bedroom variable holds the address of a Rectangle bject. length width 0.0 A Rectangle object address 0.0

7 kitchen. setLength(10. 0); kitchen. setWidth(14. 0); bedroom
kitchen.setLength(10.0); kitchen.setWidth(14.0); bedroom.setLength(15.0); bedroom.setWidth(12.0); The kitchen variable hold the address of a Rectangle object. A Rectangle object A Rectangle object Length 1 width address address 10.0 14.0 The bedroom variable hold the address of a Rectangle object. A Rectangle object A Rectangle object Length width address address 15.0 12.0

8 II. Static Class Members
It is possible to create a field or method that does not belong to any instance of a class. Such members are known as static fields and static methods. When a value is stored in a static field, it is not stored in an instance of the class. In fact, an instance of the class doesn't even have to exist in order for values to be stored in the class's static fields.

9 II. Static Class Members
Static methods do not operate on the fields that belong to any instance of the class. Instead, they can operate only on static fields. We can think of static fields and static methods as belonging to the class instead of an instance of the class.

10 II.1. Static Fields A static field is declared with the keyword static after the access specifier. There is only one copy of the field in memory, regardless of the number of instances of the class that might exist. A simple copy of a class's static field is shared by all instances of the class.

11 II.1. Static Fields Problem: Count the number of instances of a class that are created. Use a static field to keep count of the number of instances. Each time an instance of the class is created, the constructor will be called and that static field will be incremented.

12

13

14 instanceCount field (static)
II.1. Static Fields All instances of the class Countable share the static field instanceCount. instanceCount field (static) 3 object1 object2 object3

15 II.1. Static Fields Java automatically stores 0 in all uninitialized static member variables. The instanceCount field in this Countable class is explicitly initialized so it is clear to anyone reading the code that the field starts with the value 0.

16 II.2. Static Methods A static method is declared with the keyword static after the access specifier in the method header. When a class contains a static method, it is not necessary for an instance of the class to be created in order to execute the method. Static methods belong to the class and may be called without any instances of the class. In order to call a static method, we simply write the name of the class before the dot operator in the method call.

17 II.2. Static Methods

18 II.2. Static Methods

19 II.2. Static Methods Static methods are convenient for many tasks because they can be called directly from the class, as needed. They are most used to create utility classes that perform operations on data, but have no need to collect and store data. Static methods can not refer to non-static members of the class. Any method called from a static method must also static. If the static method uses any of the class's fields, they must be static as well.

20 II.2. Static Methods

21 II.2. Static Methods

22 Checkpoint 9.1 What is the difference between an instance field and a static field? 9.2 What action is possible with a static method that is not possible with an instance method? 9.3 Describe the limitation of static methods.


Download ppt "A Second Look at Classes and Objects - 1 Static Class Members"

Similar presentations


Ads by Google