An object is the thing that holds the values. You create it using new Coder()
. It has no name.
A reference variable is a thing that points to that object. You can think of it as a name that you give it.
If you just create new Coder()
and never let anything point to it it will be pointless and eventually garbage collected, because there's no way to reference it.
If you do
Coder coder1 = new Coder();
then coder1
is a reference to the object. You can use it to call methods on the object like coder1.toString()
.
You can even create another reference that points to the same object:
Coder coder2 = coder1;
It might look like you now have 2 Coder
objects, but you don't. There's still only one. You have 2 references to it (or "names" if you will). coder1.toString()
will do the exact same thing as coder2.toString()
, because they reference the same object.