Recap

Video of this lecture.

Recap

Last week we talked about

  • Classes - the ‘plan’ or ‘blueprint’ for how to make..
  • Objects - blocks of memory containing..
  • Member Variables - stored within the class and..
  • References - ‘pointers’ to objects so we can find and use them

A Simple Class

class Foo {
  int x;
  int y;
}

Foo f = new Foo();
f.x = 42;
f.y = 9;
System.out.println(f.x);

A number of things happened in one line Foo f = new Foo();

  • Foo f - create a label (or reference) called f
  • new - creates a new object
  • Foo() - using a plan (or class) called Foo
  • f = .. - attach the reference to the object

f points to object

Multiple Objects Of The Same Class

Foo f = new Foo();
f.x = 42;
f.y = 9;

Foo g = new Foo();
g.x = 66;
g.y = 25;

g.y is updated

Multiple References To A Single Object

Remember the references and the objects are different things in memory. It is possible to have multiple references all connected to a single object.

Foo f = new Foo();
f.x = 42;
f.y = 9;  

Foo g = f;
g.x = 77;

two references to a single object