Review: Classes and Object Instances ; = new (); Class #05: Objects, Memory, & Program Traces Software Engineering I (CS 120): M. Allen, 12/13 Sept. 17 We are working with both a class () and an object (actual code and data saved in memory, to which the name refers) We call the object an instance of the class Within a single program we can have more than one instance of a single class (or type of object) Each instance will be declared and instantiated separately, using distinct names Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 2 Review: Creating an Object Often, we declare and instantiate objects separately, using two distinct Java commands: ; = new (); Often, however, we do this together, in a single line: = new (); Using Multiple Objects ; = new (); = new ();.turnclockwise;.moveforward;.turncounterclockwise;.moveforward; One object is declared and instantiated (separately) Second object is declared and instantiated (all at once) Each object has its methods run separately, using its own unique identifier (name) Within a single code block, which approach we choose is often optional, since both do the same thing We will later learn about cases where this is not true Our code doesn t always need to use a single object It may be convenient (or necessary) to use multiple objects of the same (or different) type Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 3 Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 4 1
Review: What a Declaration Means A variable declaration tells the JVM/CPU to create a new identifier (name) in its memory-space Note: Eventually, we will want the variable to be the name of an actual object in some memory location. At first, however, the variable points to no actual memory location. (No memory has been set aside for an object yet, just the name.) The variable has a null reference. ; Memory null Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 5 What Instantiation Means A variable instantiation tells the JVM/CPU to: 1. Go to variable (in memory), and find another empty location in memory for it to point to (where we will store the actual object) 2. Now, create a new object and fill up the newly made slot in memory with the required data 3. Point the variable that memory location, so you can find the object later (by storing the address of the object) NOTE: to help make sure our code is free of bugs, the compiler will check that the type of object we are creating agrees with the type of variable that we declared. Here, e.g., has type. = new (); Space for Memory Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 6 The Importance of Initialization Variable Assignment Once we have actually instantiated an object of type, we can do things with it by making method calls What haps when we try to call methods on objects that do not exist yet? The code at right will not compile! ; Error: is never instantiated, but a method is called on it! Giving a variable an actual value for the first time is called initialization Once we declare and instantiate a variable, we can change its value This is called variable assignment, and has the basic syntax: objectname1 = objectname2; = new (); ; In Java this is read a very specific way: the identifier on the left now points to whatever object is given by the expression on the right When we are dealing with objects that are instances of a class (as in this example code), this moves the reference of identifier objectname1 It now points to the same memory location as objectname2 Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 7 Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 8 2
What Assignment Means A variable assignment tells the JVM/CPU: 1. Take the name on the left 2. Change what it refers to so that it is now the same as thing named on the right = ; Variable Assignment This will all work just fine, as long as objectname1 and objectname2 are both variables of the same type (that is, their classes must agree with one another): = new (); ; Question: what haps to the original object that used to name? Answer: it is orphaned, and no longer exists for use in our program Space for Space for If we try to do this with types that don t agree, the code will not compile at all: = new (); Color col = java.awt.color.blue; = col; // compiler error!! Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 9 Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 10 Variables and Their Objects When we properly instantiate an object: ; = new (); The variable name ( ) is bound to its object Each variable can only be bound to one object at a time Before the initialization on the 2 nd line of code above, the variable is bound to no object (it is null) We can also un-bind a variable so it points at no object: The that used to be bound is now an orphan Compilation Errors and Runtime Errors Many of the mistakes a coder makes will be caught when they try to compile their code, before it can ever be run Bad syntax (missing semicolons, etc.) Type errors (the wrong input parameter for a method, etc.) Even more complex things However, some errors cannot be caught at compile time Some code will compile, but then fail when it actually runs These are called runtime errors (or runtime exceptions) The Java environment (JVM) will try to signal what kind of runtime error we have, using an exception message Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 11 Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 12 3
Anatomy of a Runtime Error These error messages can be quite complex! They are meant to be helpful, however. The first line tells us what sort of error this is This is a Null Pointer Exception error Lines starting with at tell us where error is made Look for your file names and line numbers In Eclipse, you can often click on the line numbers to go there Tracing Objects,, ; = new (); = new ();.turnclockwise(); It is often useful to keep track of the objects that are bound to variables This helps in figuring out what some code does, and in de-bugging code that is not working properly If you start getting a lot of Null Pointer errors, for example, you may find doing a program trace can be very helpful! Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 13 Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 14 Tracing Objects with Object Diagrams (1) Tracing Objects with Object Diagrams (2),, ; = new (); Note: to save typing, Java allows us to declare multiple variables of = new (); the same type on one single line.,, ; = new (); = new (); The syntax is the basically same, but we can have more than one variable name.turnclockwise(); (separated by commas) after the single variable type..turnclockwise(); Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 15 Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 16 4
Tracing Objects with Object Diagrams (3) Tracing Objects with Object Diagrams (4),, ; = new ();,, ; = new (); = new (); = new ();.turnclockwise();.turnclockwise(); Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 17 Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 18 Tracing Objects with Object Diagrams (5) Tracing Objects with Object Diagrams (6),, ; = new ();,, ; = new (); = new (); = new ();.turnclockwise();.turnclockwise(); Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 19 Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 20 5
Reminder: Orphans are Unavailable Forever After the first object is let go, so no variable name is bound to it, we cannot ever use it again This allows the JVM to free up some memory space, which can be useful in large, complex programs Don t do it by accident! = new (); = new (); = ; This Week & Next Friday: in the CS Lab (16 Wing) Meetings this week: Monday Thursday: regular classroom First homework assignment posted to D2L Due by 5:00 PM, Friday 15 Sept., on D2L Reading: sections 3.1 3.10 Due start of day, next Monday (7:45 AM, 18 Sept.) Quiz 01 next Monday, 18 Sept. (end of class) Covers materials from lectures 01 03 (all on class Notes page) Office Hours: Wing 210 Monday/Tuesday, 4:30 PM 6:30 PM Thursday/Friday, 9:00 AM 10:30 AM Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 21 Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 22 6