Object References
> Click or hit Control-Enter to run Example.main above
A Reminder About Plagiarism
We catch and punish cheaters. We do this for the hard-working and honest students in the class—we’ve got your back.
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
More Class Design: abstract
Marking a class as abstract
means that it can only be extended and cannot
be instantiated:
public abstract class Pet { }
public class Dog extends Pet { }
Pet pet = new Pet(); // This will not work
Dog dog = new Dog(); // This will work
> Click or hit Control-Enter to run Example.main above
private
Classes?
In Java classes cannot be marked as private
: that would make little sense,
since nobody could use them.
-
To use it you have to create one
-
To create one you have to call one of it’s methods (the constructor)
-
But you can’t call it’s methods because the entire class is
private
Inner Classes
But we can achieve something similar using so-called inner classes:
public class Dog {
class DogFood {
public String toString() {
return "kibble";
}
}
private DogFood myFood;
Dog() {
myFood = new DogFood();
}
}
> 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);
you = new Person(); // Now you refers to a new Person object
System.out.println(you == me);
We can 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 = 10;
Copying References
class Person {
public int age;
}
Person me = new Person();
Person you = me;
me.age = 10;
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) {
this.age = setAge;
}
}
Person me = new Person(38);
Person you = new Person(18);
Swapping References
class Person {
public int age;
Person(int setAge) {
this.age = setAge;
}
}
Person me = new Person(38);
Person you = new Person(18);
Person tmp = me;
Swapping References
class Person {
public int age;
Person(int setAge) {
this.age = setAge;
}
}
Person me = new Person(38);
Person you = new Person(18);
Person tmp = me;
me = you;
Swapping References
class Person {
public int age;
Person(int setAge) {
this.age = setAge;
}
}
Person me = new Person(38);
Person you = new Person(18);
Person tmp = me;
me = you;
you = tmp;
Swapping References
class Person {
public int age;
Person(int setAge) {
this.age = setAge;
}
}
Person me = new Person(38);
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) {
this.age = setAge;
}
}
int birthday(Person toSet) {
toSet.age++;
return toSet.age;
}
Person me = new Person(38);
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) {
this.age = setAge;
}
}
int birthday(Person toSet) {
toSet.age++;
return toSet.age;
}
Person me = new Person(38);
Pass By Reference
class Person {
public int age;
Person(int setAge) {
this.age = setAge;
}
}
int birthday(Person toSet) {
toSet.age++;
return toSet.age;
}
Person me = new Person(38);
System.out.println(birthday(me));
Pass By Reference
class Person {
public int age;
Person(int setAge) {
this.age = setAge;
}
}
int birthday(Person toSet) {
toSet.age++;
return toSet.age;
}
Person me = new Person(38);
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) {
this.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) {
this.age = setAge;
}
Person(Person other) {
this.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
Announcements
-
MP2 is due Monday! Office hours all day today.
-
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!