Sept. 27th Bryan Bishop Notes object references and aliases passing objects as parameters static modifier exception modifiers interfaces nested classes and interfaaces dialog boxes GUI components events and listeners References An object reference variable holds the memory address of an object Rather than dealing with arbitrary addresses, we often depict a reference graphically as a "pointer" to an object Blah x = new Blah(); x --> is a reference to the new Blah instance The null reference an object reference variable that does not currenlty point to a no bject is called a null reference The reserved wsord null c can be used to explicity set a null reference name=null; or to check to see if a reference is currently null: if(name==null) System.out.println("invalid"); Garbage collector -> frees memory and removes objects that are not participating An object reference variable declared at the class level (an instance variable) is automatically initialized to null The programmer must carefully ensure tha an object reference variable refers to a vlalid object before it is used \ Attempting to follow a null reference causes a NullPointerException to be thrown Usually a compiler will check to see if a local variable is being used without being initialized The "this" reference allows an object to refer to itself. That is, the "this" reference, used inside a method, refers to the object through which the method is being executed Suppose the the "this" reference is used in a method called tryMe If tryMe is invoked as follows, the "this" referns refers to obj1: obj1.tryMe(); But in this case, the "this" reference refers to obj2: obj2.tryMe(); The "this" reference plays an important role in constructors of classes Assignment Revistted The act of assignment takes a copy of a value and stores it in a a variable For primitive types: num2 = num1; bishop1=bishop2; For objects, it copies the address/the reference. For primitive data types, the values are copied. bishop1.set(x,5); bishop2.get(x); // x would be "5", if bishop1=bishop2; Two or more references that refer to the same object are called aliases of each other One object (and its data) can be accessed using different reference variables Aliases can be useful, but should be managed carefully Testing Objects for Equality The == operator compares object references for equality, returning true if the references are aliases of each other. bishop1 == bishop2; A method called equals is defined for all objects, but unless we redefine it when we write a class, it has the same semantics as the == operator dot equals method compares two objects bishop1.equals(bishop2) ... check two objects .. will not work, there is a default behavior of the equals method which is the (==) operator (by default) bishop1.equals(bishop2) is the same as (bishop1==bishop2) We can redefine the equals method to retrun true wunder whatever conditions we think are appropriate How does the "this" operator work? ----- Sept. 28th The == operator compares object references for equality, returning true if the references are aliases. You have to write your own ".equals" method Parameters in a Java method are pasesd by value This means that a copy of the actual parameter (the value passed in) is stored into the formal parameter (in the method header) Passing parameters is therefore similar to an assignment statement When an object is passed to a method, the actual parameter and the formal parameter become aliases of each other. What do you do with a parameter inside a method may or may not have a permanent effect (outside the method) See ParameterPassing.java (page 253)