chapter7 - Chapter7...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Chapter 7 Object-Oriented Programming – Additional Details Object Creation - a Detailed Analysis Assigning a Reference Testing Objects For Equality Passing References as Arguments Method-Call Chaining Overloaded Methods Constructors Overloaded Constructors 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Object Creation - a Detailed Analysis Let's start the chapter with a behind-the-scenes detailed look at what  happens when a program instantiates an object and stores its address in  a reference variable. Code fragment: 1. Car car; 2. car = new Car(); 3. car.year = 2008; 1. Space is allocated in memory for the  car  reference variable. The  car   reference variable will hold the address of an object, but since there's no  object created for it yet, it doesn't yet hold a legitimate address. 2. Space is allocated in memory for a new  Car  object. The address of the  allocated space is assigned to  car . 3. The  car  variable's value (the address of a  Car  object) is used to find the  Car  object in memory, and then 2008 can be stored in the  Car  object.  Note that for this assignment to work, we're making the simplifying  assumption that  year  is a  public  instance variable. 1 2 3 4 reference variable declaration object instantiaton Assign 2008 to  car 's  year  instance variable
Background image of page 2
3 car memory Object Creation - a Detailed Analysis Code fragment: 1. Car car; 2. car = new Car(); 3. car.year = 2008; 1062 1062 2008 ? 1 2 3 4 5
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Assigning a Reference The result of assigning one reference variable to  another is that both reference variables then point to  the same object. With both reference variables pointing to the same  object, if the object is updated by one of the  reference variables, then the other reference variable  will notice that change when it attempts to access the  object. That can be disconcerting! 1 2
Background image of page 4
5 Assigning a Reference Suppose you want to create two  Car  objects that  are the same except for their color. Your plan is to  create the first car, copy the first car to the second  car, and then update the second car's  color   instance variable. Will this code accomplish that? Car stacyCar; Car johnCar = new Car(); johnCar.setMake("Toyota"); johnCar.setYear(2008; johnCar.setColor("silver"); stacyCar = johnCar; stacyCar.setColor("peach"); 1
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Assigning a Reference The problem with the previous slide's code is that the  stacyCar = johnCar;  statement causes the two references  to point to the same single  Car  object. Thus,  johnCar 's color  becomes "peach" (and that was not intended). johnCar = new Car();
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/22/2010 for the course COMPSCI CS201 taught by Professor Sorenson during the Spring '10 term at Wisconsin Milwaukee.

Page1 / 39

chapter7 - Chapter7...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online