<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">// Lecture 19
// 10/31
// mechanics of objects!

class Person {
    // instance variables
    // - called INSTANCE because not static
    // - they only exist when an object is created
    
    public String name;       // name of current Person
    public Person friend;     // friend of current Person is also a Person

    // constructor
    // use constructors to set IVs usually, but can do even more!
    public Person(String s) {
	name=s;
    } 
    
    // method for setting value of $friend$:
    public void set_friend(Person p) {
	friend = p;
    }

    // using $toString$ to print description of current object:
    public String toString() {
	return "Name of person: "+name+"; Name of friend: "+friend.name;
    }
}

public class lecture19 {
    public static void main(String[] args) {
	
	// create 4 new people:
	   Person a = new Person("Dave");
	   Person b = new Person("Jeff");
	   Person c = new Person("Nate");
	   Person d = new Person("Tony");

	// Ira's friend is Jeff
        // --------------------
	// bad style:
  	   a.friend = b;
	// better style:
	   a.set_friend(b);
	
	   // The difference becomes more apparent during discussion of
	   // information hiding and encapsulation -- you don't want to let
	   // someone else change a field directly, because someone else might
	   // be relying on the current value: using a method acts as a "guard"
	   // to prevent confusion.

        // Description of Ira:
	   System.out.println("Part 1:");
	   System.out.println(a.toString());
	   
	   // Actually, you could write just
	   // System.out.println(a)
	   // Why? $toString$ is a "builtin" method that prints
	   // whatever string returned in the body of your
	   // customized $toString$.

        // Part 1: DRAW BOX DIAGRAMS FOR THE CODE UP TO THIS LINE.

        // Creating objects as inputs and return values:
	   System.out.println("Part 2:");
	   b.set_friend(new Person("Alan"));
	   System.out.println(b);

        // Part 2: DRAW BOX DIAGRAMS FOR THE CODE UP TO THIS LINE.

	   // What I did was create a new Person object 
	   // As input to a method, the new object supplies an address
	   // You don't actually get or need a reference variable
	   // In this case, the dummy argument of set_friend $p$ acts as 
	   // the reference variable.
	   // More important: remember that a new object did get created!

        // Aliases
        // if you assign a reference to an object to another reference
        // an object gets obliterated...which one?

	   c = d; 
 
	   // + $c$ gets the address of $d$
	   // + so, the object that $c$ referred to is gone
	   // + $c$ now refers to the same object that $d$ does
	   // + changes to $c$ and $d$ affect eachother because they mean the
	   //   the same object

	   System.out.println("Part 3:");
	   c.set_friend(b);
           System.out.println(a);
           System.out.println(b);
	   System.out.println(c);
           System.out.println(d);

        // Part 3: DRAW BOX DIAGRAMS FOR THE CODE UP TO THIS LINE.

    }
    
}

/* output:
Part 1:
Name of person: Dave; Name of friend: Jeff
Part 2:
Name of person: Jeff; Name of friend: Alan
Part 3:
Name of person: Dave; Name of friend: Jeff
Name of person: Jeff; Name of friend: Alan
Name of person: Tony; Name of friend: Jeff
Name of person: Tony; Name of friend: Jeff
*/
</pre></body></html>