Let's discuss the differences between
static methods and instance methods so
firstly static methods systemic methods
are declared using a static modifier now
static methods can access instant
methods and instant variables directly
they're usually used for operations that
don't require any data from an instance
of the class from this in other words
and if you remember that this keyword is
the current instance of a class so
inside a static method we can't don't
have access to this keyword we can't use
that whenever you see a method that
doesn't use instant variables that
method should probably be declared as a
static method for example main is a
static method and it's called by the
java virtual machine when it starts the
java application in this example on
screen we've got a class called
calculator with a static method called
print sum and it just prints the sum of
two integer numbers then we've got the
main class with two static methods main
and print hello now inside main we're
calling the method print sum from the
calculator class so as you can see to
call the print sum method we just need
to type the class name in this case
calculator and in the method name in
this case print sum or in the second
example in the case of print hello we
can just type the method name with
parentheses which will automatically
call the print hello static method
because it's being invoked from a static
method itself so static methods don't
require an instance to be created we
just typed a class name dot method name
to access them let's move on now to
instance methods instance methods belong
to an instance a specific instance of a
class you know to use an instance method
we have to instantiate the class first
usually by using the new keyword is that
methods can access instance methods and
instance variables directly instance
methods can also access static methods
and static variables directly so in this
example we've got a class called dog
with a method bar
so notice here how the method bark is
not using the static camera at this time
so in other words that method is to
sustained an instance method then we've
got our main class with a method main
now inside main then main method we
first need to create an instance of dog
class and that's done with the line dog
Rex equals new dog as you can see we're
using the new keyword to create an
instance of that class and after we've
got the instance we can call the
instance method bark in this case by
typing Rex bark so the hard part here
could be deciding when to create an
instance I want to create a static
method so let's say some basic rules
that should help you decide so here
we've got a small diagram that should
help you with deciding whether you need
an instance or a static method no
instance methods are created more often
than static methods but let's see how
you'll follow this diagram so the
question that you would ask yourself
would be should have method be static so
for that question the next question
would be doesn't use any fields
instance variables in other words or
instance methods of this object this is
the proposed method you're looking to
write so if that's true in other words
it does use some fields and/or instance
methods then you'd make it an instance
method in the other scenario if the
method doesn't use or is not proposed to
use any instance variables or instance
methods in that case then you should
probably consider writing in as a static
method so generally speaking if you're
not using any fields or instance methods
with the new proposed method instead of
making that method static instead of a
regular instance method so that's the
main differences between static instance
methods let's now move on to the next
video we're going to talk a bit more
about static is tomm static and instance
variables see you in the next video.