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 why static keyword is used the static keyword is basically
used for memory management in our Java code and you can apply static keyword along
with the variables, functions, and blocks, etc. So let us go for them individual discussion
When a variable 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
It means that, irrespective of the number of objects I'm going to create, going to define
under the class, this static variable will have a certain. 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 methods can access the static members of the class only
So static methods can access only the static members of the class
Non-static members are not accessible from 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 better 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 bar. So there is one variable and this variable is of type static
and this is another variable which is a non-static that is I nt VAR so static variable we have
defined as static VAR of type I and non-static variable we have defined as VAR of the type
I so this is the constructor which will initialize the non variable bar which is which will be equal to 0 next one here we are defined another static method here we have defined one static variable here we have defined the static
method so public static void set static bar I nt x and these x will be
assigned to this static bar that is a static variable so always remember in
course 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
say bar 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 bar so that is the that is the error message you 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 the increase this method will increase the value of the
static bar by one and also value of the non-static bar that is varred 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 method can access both the static and non variables next one is a display which will print the current value of static bar and the current value of bar as well so now here we defining the class
that is a 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 we're defining two demo static class objects that is the ST1 and ST2 they're defined under the
demo static and instantiated with the constructor and the new keyword so here
we're having this ST1 dot increase ST1 dot increase so two times the static
variable will be increased here always remember so irrespective of the number of objects we are going to define under the class here is static demo only
one instance of the static variable will be defined here only one instance of the
static variable so this static word will be defined once only irrespective of the number of objects we are going to define under this
class that is the static demo as a result of that ST1 when it is increasing the
static bar by one then same same increase function will when will be called
from the ST2 it will be oh