lecture13 - Administrative Issues Enhancing Classes Lecture...

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

View Full Document Right Arrow Icon
Enhancing Classes Lecture 13 Administrative Issues • Ongoing – Program 3 due June 13 th • Today – Homework 3 assigned • Due June 11 th Object References Object References • Variables have been one of two varieties – Primitive Data Types – Object References • Object Reference Variable and the Object are two different things • Recall the two steps: – Declare an object reference variable – Then create a new object Allocation & Instantiation 1. Declaration allocates space for an object reference 2. Use of the new operator constructs a new instance of an object 3. The assignment operator stores a reference to the newly created object DeskLamp myLamp; myLamp = new DeskLamp(100); myLamp The null Reference • An object reference variable that doesn’t point to an object is a null reference • Reserved word null represents a null reference String name; // Uninitialized, so a null reference if (name == null ) System.out.println(“Name is a null reference.”);
Background image of page 1

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

View Full DocumentRight Arrow Icon
NullPointerException • Don’t call a method from a null reference NullPointerException thrown – Halts the program – Exceptions in Section 8.0 • Coming soon… String name; // Uninitialized, so a null reference System.out.println(name.length()); // NullPointerException thrown here NullPointerException • Avoid exceptions by checking for null references •U s e an if statement and the reserved word null String name; // Uninitialized, so a null reference // Use if statement to detect a null reference if (name == null) System.out.println(“Null reference, Length undefined”); else System.out.println(name.length()); Back to Object References • Object references store the memory address of an object • When declaring a variable, a null reference is allocated HondaCivic myCar; – Without instantiation, myCar is null null myCar Object References • When new is called, a new instance is created someplace in memory • After assignment, the variable points to that memory address myCar = new HondaCivic(); • “Pointer Relationship” depicted below myCar How it Works HondaCivic myCar; • Everything stored as bytes in memory myCar reference has a spot in memory – Example: myCar at 1502 null 1504 1503 1502 myCar 9103 9102 9101 9104 How it Works myCar = new HondaCivic(); •T h e new operator creates a new instance of the Car class – Reserves enough memory to hold the HondaCivic object – Constructor is called to initialize the values of any instance variables •E x am p l e –A n ew HondaCivic at starting at location 9102 and spanning many bytes null (0) 9103 9102 9101 1504 1503 1502 myCar 9104
Background image of page 2
How it Works HondaCivic myCar; myCar = new HondaCivic(); • The assignment stores the starting address of the object in the reference variable • The object reference REFERS to the location of the object –I t points to the object 9102 1504 1503 1502 myCar 9103 9102 9101 9104
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 10

lecture13 - Administrative Issues Enhancing Classes Lecture...

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

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