a second look at classes and objects chapter 9. static class members when a value is stored in a...

14
A Second Look At A Second Look At Classes And Objects Classes And Objects Chapter 9

Upload: dina-mccormick

Post on 18-Jan-2018

219 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: 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

A Second Look At Classes A Second Look At Classes And ObjectsAnd ObjectsChapter 9

Page 2: 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

Static Class MembersStatic Class MembersWhen 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.

Page 3: 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

Static FieldsStatic FieldsWhen 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

Page 4: 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

Static MethodsStatic MethodsWhen 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

Page 5: 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

Passing Objects As Passing Objects As Arguments to MethodsArguments to MethodsTo 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.

Page 6: 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

Returning Objects from Returning Objects from MethodsMethodsA method can return a reference

to an object. See example

Page 7: 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

The The toStringtoString method methodMost 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

Page 8: 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

Writing an Writing an equalsequals method methodYou 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

Page 9: 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

Methods that Copy Methods that Copy ObjectsObjectsYou 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

Page 10: 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

Copy ConstructorsCopy ConstructorsAnother 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

Page 11: 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

AggregationAggregationAggregation 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.

Page 12: 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

AggregationAggregationSuppose 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 name2.The instructor’s last name, first

name, and office number3.The textbook’s title, author, and

publisher

Page 13: 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

AggregationAggregationA 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

Page 14: 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

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 Aggregation in UML diagramsdiagrams