Let's discuss the differences now
between static variables and instance
variables so firstly static variables by
using the keyword static the static
variables are also known as static
member variables every instance of the
class shares the same static variable so
if changes made to that variable all
other instances of that class will see
the effect of that change static
variables aren't used very often but can
sometimes be very useful for example
when reading user input using scat the
ER scanner class we can declare scanner
as a static variable and that way all
the static methods can access that
scanner directly now in the following
example we've got a class called dog and
it's got a static variable called name
now there's a constructor that sets the
static variable to the parameter value
passed to the constructor and we've got
a method there called print name which
isn't static so that's a pretty simple
class and inside main we're creating two
instances of the dog class with the line
dog Rex equals new dog in parentheses
double quotes Rex we're creating an
instance of the dog class and then we're
passing the string Rex as a parameter
and that'll be the name of the dog in
the next line we've got a similar
situation just passing the parameter at
the parameter which will be used as the
name as fluffy so they'll be calling the
print name method on both of the
instances so they're just regular
instance methods because they aren't so
defined using static so both method
calls will print
fluffy so you might be wondering why is
that the case why would both methods
here print fluffy well remember that
static variables are shared between
instances so in other words once we
change the static variable all instances
will see that change we made so what we
call a constructor with parameters
fluffy it modified the static variable
name and because both instances are
sharing that variable that's why it
prints fluffy twice so you could also
say that all dogs have the same name but
that's logically incorrect in real life
of course every dog has a different name
it's going to breed and so on so
hopefully now you can see how static
variables can be used in a
really sometimes as in this example when
perhaps you were assuming that the dog's
name would change with each instance of
the class so that would be a scenario
where using a static variable probably
wouldn't be a good idea using a regular
instance variable would make a lot more
sense in this particular example all
right so let's move on now to instance
variables so firstly they don't use the
static keyword when you're defining them
now they're also known as fields or
member variables unlike a static
variable instance variables belong to a
specific instance of a class each
instance has its own copy of an instance
variable so every instance can have a
different value or state and instance
variables represent the state of a
specific instance of a class let's see
an example using instance variables so
in this example we've again we've got a
very similar code to what we looked at
earlier in the video but this time the
variable name in the dog class isn't
static so it's just a regular instance
variable so once again the constructor
is setting the value from the parameter
passed to that constructor so now the
card will print Rex and on the next line
fluffy and that's because we're using
instance variables and each instance of
the class has its own state or own
values for any very variables that have
been defined so in other words now there
be dogs got its own copy of the name
variable we can also say that it's
basically not shared like it was before
in the earlier example he's using a
static variable so in most cases you'd
probably want to use instance variables
but there will be scenarios when it can
be useful to use a static variable and
in the earlier example and previously we
talked about using a scanner as a static
variable so that it could be shared
across any static methods when you're
reading user input
so that's instance and static variables
let's move on now to the next video.