©2004 Brooks/Cole Chapter 10 More on Classes
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Object assignment With primitive types, setting one variable equal to another copies the value of the variable on the right into the variable on the left. With objects, what is copied is the address of the object –there is only one object
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Two Book objects After Assignment book2 = book1
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Making Copies of Objects To copy an object you need a method –Copy constructor public Date( Date original) –A method public void setEqual( Date d) –Override the clone method from the Object class
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Scope Re-visited Variables declared inside a method are local to the method Method parameters are local to the method Variables declared in the body of a class (outside on any method) are visible in all the methods of the class
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 An Example of Scopes
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Static Members Variables declared with the static modifier are shared by all objects that belong to the class Methods declared with a static modifier do not have any instance data associated with them
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sharing the Static Data Member taxRate
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The Storage of Two Date Objects in Memory