In this video, we're just gonna go over and talk
about references versus objects versus
instances versus classes. Now, by now
you've probably noticed that these words
have been used in this section, reference
object instance in class. Now these new
concepts may well be confusing at first.
So in this video I'm going to try and go
through the process and show you exactly
what each of these words actually mean
in the context of Java programming.
Alright, so let's start by using the
analogy of building a house to
understand classes. Now a class is
basically a blueprint for a house,
so thinking back to the physical world,
so you use the blueprint or the plans for a
house using that, we can build as many
houses as we like based on those plans.
Now each house you build in other words
back in programming terms each house you
would instantiate using the new
operator is an object which can also be
known as an instance or is known as an
instance. Getting back to the physical
world, each house you build has an
address it's built at a physical
location, in other words if you want to
tell someone where you live you give
them your address perhaps written on a
piece of paper. So that that piece of
paper if your address on it is known as
a reference. Now you can copy that
reference as many times as you like but
there is still just one house that
you're referring to. In other words, we're
copying the paper that has the address
on it not the house itself all we're
writing that address on another piece of
paper. Now back to programming terms,
we can pass references as parameters to
constructors and methods. Let's now just
go a little bit deeper to about to make
this a little bit more clearer. Alright,
so we've got some code on the screen now.
Now on the left hand side we've got the
class house with an instance variable
also known as a field called color now
on the right hand side we have the main
class with the main method. Now this code
in the main method is creating instances
of the house class, changing the color
and printing out the result. So let's
actually go through line by line and see
what happens when this code is executed.
Alright, so looking at this first line
that I've got highlighted in red into
the right hand side, the house blueprint
equals new house blue in parentheses and
double quotes, well that creates a new
instance of the house class.
So remember house is a blueprint and
more assigning it to the blue house
variable, so in other words it's a
reference to the object in memory and
you can see that hopefully on the left
hand side that it makes a bit more sense
there, blue house is the variable we're
creating a new instance of the house
class and assigning it the color blue.
Alright so this next line that I've
got highlighted house another house
equals blue house that creates another
reference to the same object in memory.
So what we've got now is two references
pointing to the same object in memory,
so let's do one house but two references to
that one object, so in other words we've
got two pieces of paper with the
physical address of where the house is
built, written down if we came back to a
real-world example to make that clearer.
So next on the right-hand side I've got
two print line statements highlighted,
so they print the value of the color
variable for blue house and another
house. Now in this scenario, both will
actually print blue and that's because
again, we've got two references to the
same object and again you can see on the
left-hand side there, hopefully that
image makes that clear as to how this is
actually working. Alright, so this next
line another house dot set color
parentheses in double quotes yellow,
well that's calling the method set color
and setting the color to yellow. Now to
the left you can see that both blue
house and another house now have the
same color, now why is that?
Well, remember that we've got two references
that point to the same object in memory.
Now once we change the color of one,
both references still point to the same
object, so consequently they've both got
the same value of yellow, in this example.
Now in a real world example there's
still just one physical house at that
one address even though we've written
the same address on two pieces of paper.
So if you go here then we could paint
the house yellow and obviously both
references to the physical house still
point to a house that is yellow.
Alright so we've got a couple more print
line statements here, so these two print
line statements are printing the same
color, both are printing yellow because
again we've got two references that
point to the same object in memory and
again you can see that to find with the
arrows on the left-hand side of the
image, one instance pointed to by two
objects. Okay, so moving on what we're
doing now is with that next line house
green house equals new house parenthesis
double quotes green.
Well, we're creating another new instance
of the house class, this time with the
color set to green. So now we've got two
objects in memory, but we've got three
references and those references are blue
house, another house and green house.
Now the variable or reference green house
points now to a different object in
memory but blue house and another house
point to the same object in memory
at this point. Alright, so this statement
another house equals green house, so what
we're doing here is we're assigning
green house to another house, so in other
words, we're dereferencing another house
and it will now point to a different
object in memory. Now previously it was
pointing to a house that had the yellow
color but now it's pointing to a house
that's got the green color, so in this
scenario we still have three references
and two objects in memory but blue house
points to one object while another house
and green house now point to the same
object in memory. And finally we've
got those last three print line
statements, the first will print yellow
since the blue house variable or
reference points to the object in memory
that has the yellow color, well the next
two lines will print green and that's
because another house in green house
point to the same object in memory.
So keep in mind that in Java, you always
have a references to an object in memory.
There's no way to access an object
directly, everything is done using that
reference.
Alright so hopefully that now
made it a little bit clearer, let's move
on to the next video.