java 3
java 3
CMSC 202
Object Creation
• Objects are created by using the operator new in
statements such as…
Car c = new Car();
3
The (Almost) Finished Car Class
public class Car {
private int numLiters;
private int horsepower;
private int numDoors;
private int year;
private String color;
private String model;
private String make;
private String vin;
7
Copy Constructor
• Another common form of a constructor is
called a copy constructor
• A copy constructor takes a single argument
that is the same type as the class itself and
creates a copy of it…
// copy constructor
public Car(Car otherCar) {
this(otherCar.vin, otherCar.color, otherCar.make,
otherCar.model, otherCar.numLiters,
otherCar.horsepower, otherCar.numDoors,
otherCar.year);
}
8
Better Car Constructors
// a constructor that uses parameters and default state values
public Car(String vin, int year, String make, String model) {
this(vin, "blue", make, model, 2, 200, 2, year);
}
// a default constructor
public Car() {
this("1234567", "blue", "Ford", "Focus", 2, 200, 2, 2011);
}
10
Main Memory
• The stack grows and shrinks as needed.
(why?)
• The heap also grows and shrinks. (why?)
• Some of memory is unused. (“free”)
11
Object Creation
• Consider this code that creates two Cars:
Car c1, c2;
c1 = new Car("A", 2000, "Ford", "Explorer");
c2 = new Car("B", 2009, "Nissan", "Titan");
12
Objects in Memory
• The following creates two local variables on the stack…
Car c1, c2;
c1 2000 2011
… Unused Memory Ford Nissan …
c2 Explorer Titan
Stack Heap 13
Why We Care (1 of 4)
• Given the previous code and corresponding picture of memory…
Stack Heap
c1 Unused
2000
Car c1 = new Car("A",2000,"Ford","Explorer"); … Ford …
Memory
Car c2 = c1; c2 Explorer
Stack Heap
15
Why We Care (3 of 4)
• Consider this code and the changing picture of
memory…
16
Why We Care (4 of 4)
2011 2000
Unused
… c1 Memory
Nissan Ford …
Titan Explorer
Stack Heap
19
Variables Review:
Primitives vs. References
• When the variable is a class type, only the
memory address (or reference) where its object
is located is stored in the memory location
assigned to the variable (on the stack).
• The object named by the variable is stored in the
heap.
• Like primitives, the value of a class variable is a
fixed size.
• The object, whose address is stored in the
variable, can be of any size.
20
Class Parameters
• All parameters in Java are pass-by-value parameters.
– A parameter is a local variable that is set equal to the
value of its argument.
– Therefore, any change to the value of the parameter
cannot change the value of its argument.
21
Class Parameters
• The value plugged into a class type parameter
is a reference (memory address).
– Therefore, the parameter becomes another name
for the argument.
– Any change made to the object referenced by the
parameter will be made to the object referenced
by the corresponding argument.
– Any change made to the class type parameter
itself (i.e., its address) will not change its
corresponding argument (the reference or
memory address).
22
Change Car Example
public class CarParameterTest {
private static void installTurbocharger(int horsepower) {
horsepower = horsepower + 20;
}
private static void changeCar1(Car car) {
car = new Car("XYZ456", 2011, "Audi", "A8");
}
private static void changeCar2(Car car) {
car.setStyle("Audi", "A8");
}
public static void main(String[] args) {
Car car = new Car("ABC123", 1995, "Ford", "Mustang");
installTurbocharger(car.getHorsepower());
System.out.println(car); // output?
changeCar1(car);
System.out.println(car); // output?
changeCar2(car);
System.out.println(car); // output?
}
}
23
Use of = and ==
with Variables of a Class Type
24
The Constant null
• null is a special constant that may be assigned to a
reference variable of any class type.
YourClass yourObject = null;
25
Anonymous Objects
• Recall, the new operator
– Invokes a constructor which initializes an object, and
– Returns a reference to the location in memory of the
object created
• This reference can be assigned to a variable of the
object’s class type.
• Sometimes the object created is used as an argument
to a method, and never used again.
– In this case, the object need not be assigned to a variable,
i.e., given a name.
• An object whose reference is not assigned to a variable
is called an anonymous object.
26
Anonymous Object Example
• An object whose reference is not assigned to a
variable is called an anonymous object.
• An anonymous Car object is used here as a
parameter:
Car myCar = new Car("ABC123",2000,"Ford","Explorer");
if(myCar.equals(new Car("ABC123",2000,"Ford","Explorer"))) {
System.out.println("Equal!");
}