Reasoning About References in Java

Suppose we have

class C
{
...
}
...
C ob1, ob2;
...

What does an assignment

ob1 = ob2;

mean?

 

Object variables  really declare references  to objects implicitly.

References behave as "pointers".

Reference assignment

ob1 = ob2;

says

"Make ob1 point to where ob2 points."

 

There may be other  references to ob1 not shown.

When there are no  more references, the object can be garbage-collected : its space in memory is recycled to other uses.

 

 

Before:

After:

Caution: Any modification to ob2 will now be "felt" by ob1.

 

To Next Slide To Previous Slide To Contents