in this video we are going to discuss
static keyword in Java static keyword we
usually use in our java code for the
memory management static keyword can be
used with a variable with the function
and also with a block so let us discuss
more on this static keyword while static
keyword is used the static keyword is
basically used for memory management in
our java code and you can apply steady
keyword along with the variables
functions and blocks etcetera so let us
go for them
individual discussions when available is
static it acts like a class variable for
the multiple number of objects only one
memory space will be occupied against
the static variable when you define one
static variable within the class then it
will become a class variable means that
irrespective of the number of objects
I'm going to create going to define
under the class the static variable will
have a single instance in the computer's
memory so that means it is behaving as
if it is a class variable and that is a
very special feature here static methods
so static methods can be called without
creating an object under the class so
without defining any object under the
class I can use the static method using
the class name dot operator so static
mate may be thoughts can access the
static members of the class only so
static methods can access only the
static members of the class non-static
method form the static method a normal
method cannot access the static members
of a class so these are the respective
features with the static keyword so let
us go for one practical demonstration on
this for the bigger understanding in
this demonstration we shall discuss the
keyword static in our Java programming
now what is the static keyword so here
we are having one class the name of the
class is static demo and it is having
int static part so that is one variable
and this variable is of type static and
this is another variable which is a non
static that is int bar so static
variable we have defined
a static part of type int and non-static
variable web defined has heard of the
type int so this is the constructor
which will initialize the non-static
variable part which is which will be
equal to zero next one here we defined
another static method here we have
defined one static variable here we have
defined the static method so public
static void set static part int X and
this X will be assigned to this static
part that is a static variable so always
remember in case of static method they
can access only the static variables
non-static variables cannot be accessed
from here so if I want to write this one
as same Hart say is equal to 100 if I
write here so it is having one error
message is coming and that error message
is telling that cannot make a static
reference to the non-static field part
so that is the that is the error message
we are getting because non static method
cannot access static method cannot
access non-static variables so that is
very important next method is known as
increase this method will increase the
value of the static part by one and also
value of the non-static part that is far
as one so in this way two variables have
got increased here so we have got one
lesson that static method can access
only the static variables but non static
method can access both the static and
non-static variables next one is a
display which will print the current
value of static part and the current
value of heart as well so now here we're
defining the class that is the keyword
static which name is same as that of the
file name here so under this particular
keyword static we're defining the main
function were defining to demonstrate a
clear objective one and Institute they
are defined under the class demo static
and instantiated with the constructor
and the new keyword so here we are
having this s T 1 dot increase s T 1 dot
increase so 2 times the static variable
will be increased here always remember
so irrespective of the number of objects
we
to define under the class here is static
demo only one instance of the static
variable will be defined here so only
one instance of the static variable so
this static var will be defined once
only irrespective of the number of
objects we're going to define under this
class that is the static demo as a
result of that st1 when it is increasing
the static part by one then same same
increased function will when will be
called from the st2
it will it will be also increasing the
same static var by one but as the var is
a non-static variable so it will have a
two separate instance with respect to s
t1 and SP two separately so as a desert
that when we are calling this s T 1 dot
increase the bar which is getting
increased two times because we have
called this this function increase twice
so this instance will be totally
different from this s to do dot part
when we are calling this increase for
three times so another new instance of
what will remain created against this is
t2 object and that will get increased
for three times so I think now the
conception will be cleared if I go for
the execution so the value of the static
variable is 2 and value of the
non-static variable is 2 here because
here we have call this s T 1 dot
increase two times so the static and the
non static both the variables have got
increased by one and one and initially
their values were zero here because we
have initialize this one as zero and
automatically the static bird will be
instantiated with the value 0 it will be
initialized with the value 0 initially
so st1 dot display is displaying that
both the variables are having a value
two and two
now from this estatal this increased
function has got called thrice so the
static variable will get increased
starting from 2 so 2 to 3 3 to 4 and 4
to 5 but this bar will have will be
having a separate instance for the new
object esta 2 so it will be increased
from 0 to 1 1 to 2 and 2 to 3 as a
result of that when you are going for s2
to door display
we are finding that the static variable
is having the value 5 and the non-static
variable part is having the value 3 here
here we are having the set aesthetic bar
so here we are putting this 50 and 50
will be assigned to this static variable
so if I print this particular variable
we can we can have this one as 50 you
can find that we're having this one as
50 so this this particular method static
method has been accessed from the class
name directly from the class name
directly it can be accessed and this
static variable can be accessed from the
class name now if I use this one as st-1
if is this one as st one on also here as
st one
you can find that there they are
executable and we are getting the same
output here static minimal without
object is 50 so now if I call it from
the st2 also they are accessible because
they are static so irrespective of the
number of objects were defining under
the class the static demo these two
static members variables and methods
will have a single instance in the
computer's memory and they can be
accessed either using object name dot or
using class limit or directly so in this
way we have shown you that how what is
the static how to use a static keyword
in our java code
we are going to get the same output here
thanks for watching this video