Overview (1): Before We Begin Some administrative details Some questions to consider A Closer Look at References Class Implementation II Thursday, May 11 2006 The Person class (from textbook) Class objects as parameters Copy constructor Accessor and mutator methods Mutable and immutable classes CSE 1030, Summer 2006, Administrative Details (1): Exercise PEx01 Before We Begin Due Friday, May 12 Although you are not graded for this, you should still submit your solution! No Lecture May 16 I will be in Europe away for a conference from May 12-17 Will respond to email but there may be a delay! I will re-schedule this lecture towards the end of the semester Administrative Details (2): Congress of the Humanities and Social Sciences 2006 at York Saturday, May 27 to Saturday, June 3, 2006 Classes (including unfortunately CSE 1030) will not be held on any York Campus from Monday, May 29 to Saturday, June 3, nor will classes be provided by Internet or correspondence during this period Some Questions to Consider (1): What is a static method / variable? What restrictions must we be aware of when implementing a static method and how can we overcome this? How is memory organized? What is a reference? How are objects represented in memory? In Java, ca we directly access/manipulate a reference?
The Person Class (1): Class From your Textbook A Closer Look at References We will use it to examine various of the concepts that we will discuss today Page 296 Brief Description Describes basic data about people in places such as author listings, library catalogues etc. Person s name Date of birth Date of death Brief Description (cont.) All instance variables are of a class type no primitive type instance variables Person s name String Date of birth Date class Date of death Date class (null if person is still alive) Lets take a look at the code of both the Person and Date class Notice how in the equals method, we call the equals method of the instance variable objects Some Comments Contains no no-argument constructor why? But shouldn t all classes contain a no-argument constructor? Does it make sense to have a no-argument constructor for the Person class? Think about what it means to be a person Class Invariant A statement that is true of every object of the class created by a constructor All other methods of the class preserve the truth of this statement After executing a method, still holds true With respect to the Person class, what is the class invariant? What is true about every instantiated object of the Person class? Class Parameters (1): Reference Method Parameters As you are aware, in addition to primitive types, a method can also have class parameters The caller to such a method can pass an object to it Important we are not really passing the actual object itself but rather, we are passing the reference that points to the object This actually can lead to some subtleties When working with reference parameters, you may not always achieve what you intended!
Class Parameters (2): Reference Method Parameters (cont.) Reference parameters are also passed-by-value as with the primitive types Recall that with call-by-value, the parameter itself within the method is a local variable in other words, we have a local variable that points to some object Lets look at an example Once again, the ToyClass Lets create a main method that creates a ToyClass object and calls its methods Class Parameters (3): 3078 Reference Method Parameters (cont.) Mr. Cellphone 0 Before the call to the change method aparameter 3078 Mr. Cellphone 0 Before the call to the change method aparameter 3078 Hot Shot 42 After the call to the change method Class Parameters (4): Reference Method Parameters What implications do reference parameters have with respect to mutator and accessor methods? Copy Constructor (1): A Special Type of Constructor Single parameter of the same type as the class itself Lets look at an example Date class Should create an public Date(Date adate) object that is a if (adate == null)//not a real date. separate and System.out.println("Error."); independent object but with instance variables that are equal (exact month = adate.month; copy) of the parameter year = adate.year; object Copy Constructor (2): Date date1 = new Date( January, 1, 2006); Date date2 = new Date(date1); public Date(Date adate) if (adate == null) After above code is executed both date1 and date2 represent the System.out.println( Error! ); same date (Jan. 1 2006) but they are two different objects. In month = adata.month; other words, if we change date1, date2 is not changed and viceversa (date2 is the caller and year = adate.year; date1 is an argument) Copy Constructor (3): Lets modify the Date class by adding an additional instance variable of a class type (assume some class called Weather) Date date1 = new Date( January, 1, 2006); Date date2 = new Date(date1); What is wrong with this copy constructor definition? Is it correct? Consider the following date2.weather = null; public Date(Date adate) if (adate == null) System.out.println( Error! ); month = adata.month; year = adate.year; weather = adate.weather;
Copy Constructor (4): The previous code segment is an example of a nonsafe copy constructor We simply copied the weather instance variable but all we really did was copied the reference But the copy and the original reference point to the same object (one object) What we need to do is make a copy of the object weather = new Weather() Create a new object and then copy the values Accessor and Mutator Methods (1): Privacy Leaks As with the issue we discussed with the copy constructor, a similar problem can arise with mutator and accessor methods Lets recall an accessor method that returns the value of a primitive type Rectangle class (getwidth) We simply return the value of the primitive type instance variable public int getwidth() return awidth; Accessor and Mutator Methods (2): What if the accessor method returns a non-primitive type (e.g., class type)? Lets look at two ways to handle this Person class (getbirthdate method) Which of the two implementations is correct and why? Accessor and Mutator Methods (3): Lets assume we chose the second one Consider the following code segment Person citizen = new Person( Joe Citizen, new Date( January, 1, 1900), new Date( January, 1, 1990)); Date mydate = citizen.getbirthdate(); mydate.setdate( April, 1, 3000); return new Date(born); return born; We have basically circumvented the private declared instance variable We have changed the birth date of the Person object to a date after the death impossible! Accessor and Mutator Methods (4): But mydate refers to the private instance variable! we have changed the value of a private declared instance variable! When returning a class type instance variable, be sure to avoid this memory leak Return a copy of the class - not the reference! Accessor and Mutator Methods (5): The same of course applies to a mutator method It is not enough to simply copy the parameter class type (reference) Rather, we must make a copy of the object that the reference parameter points to
Mutable & Immutable Classes (1): But what about the method getname of the Person class? Why does it return the reference of the private instance variable name (of type String)? This is inconsistent??? return name; Mutable & Immutable Classes (2): Basically, the String class does not have any mutator methods doesn t contain any methods that can change any data of a String once it has been created Even though it seems like we can String greeting = Hello ; greeting = greeting + Goodbye ; But haven t we changed the string greeting after executing the second line? No! Rather a new object is created and reassigned to the greeting variable Mutable & Immutable Classes (3): Immutable Class A class that contains no methods (other than the constructor) that change any of the data in an object of the class Objects of such classes are know as immutable objects Safe to return a reference to an immutable object since its data cannot be changed The String class is an example of an immutable class Mutable & Immutable Classes (4): Mutable Class A class that contains public methods that can change the data of an object of the class Objects of such classes are known as mutable objects Should avoid writing methods that return mutable objects can lead to memory leaks! In fact, you should never write such methods! Exercise PEx01 (1): Any Questions? Exercise PEx01