Object References
> Click or hit Control-Enter to run Example.main above
Next Few Classes
-
Today: object references
-
Friday: return to polymorphism, which will make more sense after we talk about references, and a bit of additional class design
> Click or hit Control-Enter to run Example.main above
References
Reference: a value that enables a program to indirectly access a particular datum, such as a variable’s value or a record, in the computer’s memory or in some other storage device. The reference is said to refer to the datum, and accessing the datum is called dereferencing the reference.
Java Reference Variables
class Person { }
/*
* me is declared to hold a reference to an object of type Person,
* but currently refers to nothing.
*/
Person me;
/*
* Initializing an instance to null is another way of indicating
* that it currently refers to nothing.
*/
Person you = null;
me = new Person(); // Now me refers to a new Person object
you = me; // Now me and you refer to the same Person object
System.out.println(you == me); // The variables store the same reference
you = new Person(); // Now you refers to a new Person object
System.out.println(you == me);
We can (and will) refer to a Java variable that refers to an object as a reference variable.
> Click or hit Control-Enter to run the code above
References Are Not Objects
References are not the thing the refer to.
What are some real-world examples of references?
-
A phone number: which refers to a phone
-
A street address: which refers to a physical location
-
A social security number: which refers to a person
Copying References
Copying a reference does not copy the object it refers to.
-
Copying a phone number doesn’t copy the phone. Anyone with the number can call the same person.
-
Copying a street address doesn’t copy the location. Anyone with the address can navigate to the same spot.
-
Copying a social security number doesn’t copy the person 1. Anyone with social security number may be able to impersonate that person.
> Click or hit Control-Enter to run the code above
Copying References
class Person {
public int age;
}
Person me;
Copying References
class Person {
public int age;
}
Person me = new Person();
Copying References
class Person {
public int age;
}
Person me = new Person();
Person you = me;
Copying References
class Person {
public int age;
}
Person me = new Person();
Person you = me;
me.age = 40;
Copying References
class Person {
public int age;
}
Person me = new Person();
Person you = me;
me.age = 40;
System.out.println(you.age);
> Click or hit Control-Enter to run the code above
Swapping References
class Person {
public int age;
Person(int setAge) {
age = setAge;
}
}
Person me = new Person(40);
Person you = new Person(18);
Swapping References
class Person {
public int age;
Person(int setAge) {
age = setAge;
}
}
Person me = new Person(40);
Person you = new Person(18);
Person tmp = me;
Swapping References
class Person {
public int age;
Person(int setAge) {
age = setAge;
}
}
Person me = new Person(40);
Person you = new Person(18);
Person tmp = me;
me = you;
Swapping References
class Person {
public int age;
Person(int setAge) {
age = setAge;
}
}
Person me = new Person(40);
Person you = new Person(18);
Person tmp = me;
me = you;
you = tmp;
Swapping References
class Person {
public int age;
Person(int setAge) {
age = setAge;
}
}
Person me = new Person(40);
Person you = new Person(18);
Person tmp = me;
me = you;
you = tmp;
// Now we can discard tmp
> Click or hit Control-Enter to run the code above
Pass By Reference
class Person {
public int age;
Person(int setAge) {
age = setAge;
}
}
int birthday(Person toSet) {
toSet.age++;
return toSet.age;
}
Person me = new Person(39);
System.out.println(birthday(me));
System.out.println(me.age);
In Java methods receive a copy of a reference to the passed object.
So they can modify the object the reference refers to.
Pass By Reference
class Person {
public int age;
Person(int setAge) {
age = setAge;
}
}
int birthday(Person toSet) {
toSet.age++;
return toSet.age;
}
Person me = new Person(39);
Pass By Reference
class Person {
public int age;
Person(int setAge) {
age = setAge;
}
}
int birthday(Person toSet) {
toSet.age++;
return toSet.age;
}
Person me = new Person(39);
System.out.println(birthday(me));
Pass By Reference
class Person {
public int age;
Person(int setAge) {
age = setAge;
}
}
int birthday(Person toSet) {
toSet.age++;
return toSet.age;
}
Person me = new Person(39);
System.out.println(birthday(me));
System.out.println(me.age);
> Click or hit Control-Enter to run Example.main above
Arrays Store Object References
class Person {
public int age;
Person(int setAge) {
age = setAge;
}
}
Person[] people = new Person[4];
for (int i = 0; i < people.length; i++) {
people[i] = new Person(18 + i);
}
Person[] samePeople = new Person[4];
for (int i = 0; i < people.length; i++) {
samePeople[i] = people[i];
}
for (int i = 0; i < people.length; i++) {
people[i].age++;
}
for (int i = 0; i < samePeople.length; i++) {
System.out.println(samePeople[i].age);
}
So copying an array as above only copies the object references, not the objects themselves.
> Click or hit Control-Enter to run Example.main above
How To Copy Objects
public class Person {
public int age;
Person(int setAge) {
age = setAge;
}
Person(Person other) {
age = other.age;
}
}
If we want to copy an object, we have a few options:
-
Object
provides aclone
method -
You can implement a copy constructor as shown above
Objects v. References
This can be confusing at first, but here’s a cheat sheet that may be helpful:
-
Objects are only created when you see
new
. -
The variables that store objects actually store object references.
More Class Design: final
Marking a class as final
means that it cannot be extended:
public class Pet { }
public final class Dog extends Pet { }
public class BigDog extends Dog { } // This won't work
> Click or hit Control-Enter to run Example.main above
> Click or hit Control-Enter to run Example.main above
Announcements
-
Wednesday 5–8PM office hours have been canceled. We will now hold office hours on Fridays from 5–8PM.
-
I have office hours as usual today, 1–3PM, Siebel 2227. Please come by and say hi!
-
We have a anonymous feedback form to the course website. Use it to give us feedback!