Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Second Look At Classes And Objects Chapter 9. Static Class Members When a value is stored in a static field, it is not in an object of the class. In.

Similar presentations


Presentation on theme: "A Second Look At Classes And Objects Chapter 9. Static Class Members When a value is stored in a static field, it is not in an object of the class. In."— Presentation transcript:

1 A Second Look At Classes And Objects Chapter 9

2 Static Class Members When a value is stored in a static field, it is not in an object of the class. In fact, an object of the class does not even have to exist in order for values to be stored in the class’s static fields. You can think of static fields and static methods as belonging to the class instead of an object of the class.

3 Static Fields When a field is declared with the key word static, there will be only one copy of the field in memory, regardless of the number of objects of the class that might exist. A single copy of a class’s static field is shared by all objects of the class. See example

4 Static Methods When a class contains a static method, it isn’t necessary for an object of the class to be created in order to execute the method. See example

5 Passing Objects As Arguments to Methods To pass an object as a method argument, you pass an object reference. See example When writing a method that receives the value of a reference variable as an argument, you must take care not to accidentally modify the contents of the object that is referenced by the variable.

6 Returning Objects from Methods A method can return a reference to an object. See example

7 The toString method Most classes can benefit from having a method named toString, which is implicitly called under certain circumstances. Typically, the method returns a string that represents the state of an object. See example

8 Writing an equals method You cannot determine whether two objects contain the same data by comparing them with the == operator. Instead, the class must have a method such as equals for comparing the contents of objects. See example

9 Methods that Copy Objects You can simplify the process of duplicating objects by equipping a class with a method that returns a copy of an object. You cannot make a copy of an object with a simple assignment statement: Stock company1 = new Stock(“XYZ”, 9.62); Stock company2 = company1; See example

10 Copy Constructors Another way to create a copy of an object is to use a copy constructor. A copy constructor is simply a constructor that accepts an object of the same class as an argument. It makes the object that is being created a copy of the object that was passed as an argument. See example

11 Aggregation Aggregation occurs when an instance of a class is a field in another class. In real life, objects are frequently made of other objects. A house is made of door objects, window objects, wall objects, and much more. It is the combination of all these objects that makes a house object. When designing software, it sometimes make sense to create an object from other objects.

12 Aggregation Suppose you need an object to represent a course that you are taking in college. You decide to create a Course class, which will hold the following information: 1. The course name 2. The instructor’s last name, first name, and office number 3. The textbook’s title, author, and publisher

13 Aggregation A good design principle is to separate related items into their own classes. In this example an Instructor class could be created to hold the instructor-related data and a TextBook class could be created to hold the textbook-related data. Instances (objects) of these classes could then be used as fields in the Course class. See example

14 Course - courseName : String - Instructor : Instructor - textBook : TextBook + Course(name : String, instr : Instructor, text : TextBook) + getName() : String + getInstructor() : Instructor + getTextBook() : TextBook + toString() : String TextBook - title : String - author : String - publisher : String + TextBook(title : String, author : String, publisher : String) + TextBook(object2 : TextBook) + set(title : String, author : String, publisher : String) : void + toString() : String Instructor - lastName : String - firstName : String - officeNumber : String + Instructor(lname : String, fname : String, office : String) +Instructor(object2 : Instructor) +set(lname : String, fname : String, office : String): void + toString() : String Aggregation in UML diagrams


Download ppt "A Second Look At Classes And Objects Chapter 9. Static Class Members When a value is stored in a static field, it is not in an object of the class. In."

Similar presentations


Ads by Google