Java Essentials - Static keyword in java
2K views
Oct 24, 2024
Java Essentials - Static keyword in java Watch more Videos at https://www.tutorialspoint.com/videotutorials/index.htm Lecture By: Mr. Arnab Chakraborty, Tutorials Point India Private Limited
View Video Transcript
0:00
in this video we are going to discuss static keyword in Java static keyword we
0:06
usually use in our Java code for the memory management static keyword can be
0:11
used with a variable with the function and also with a block so let us discuss
0:17
more on this static keyword why static keyword is used the static keyword is basically
0:24
used for memory management in our Java code and you can apply static keyword along
0:29
with the variables, functions, and blocks, etc. So let us go for them individual discussion
0:37
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
0:47
When you define one static variable within the class, then it will become a class variable
0:51
It means that, irrespective of the number of objects I'm going to create, going to define
0:56
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
1:04
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
1:20
So static methods can access the static members of the class only
1:25
So static methods can access only the static members of the class
1:30
Non-static members are not accessible from the static method. A normal method cannot access the static members of a class
1:39
So these are the respective features with the static keyword. So let us go for one practical demonstration on this for the better understanding
1:48
In this demonstration, we shall discuss the keyword static in our Java programming
1:54
Now what is the static keyword? So here we are having one class, the name of the class is static demo
2:00
And it is having INT static bar. So there is one variable and this variable is of type static
2:07
and this is another variable which is a non-static that is I nt VAR so static variable we have
2:13
defined as static VAR of type I and non-static variable we have defined as VAR of the type
2:19
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
2:35
method so public static void set static bar I nt x and these x will be
2:42
assigned to this static bar that is a static variable so always remember in
2:47
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
2:57
say bar say is equal to 100 if I write here so it is having one error message is
3:03
coming and that error message is telling that cannot make a static reference to
3:07
the non-static field bar so that is the that is the error message you are getting
3:12
because non-static method cannot access static method cannot access non-static variables so that is very important
3:21
next method is known as the increase this method will increase the value of the
3:27
static bar by one and also value of the non-static bar that is varred as one so in
3:32
this way two variables have got increased here so we have got one lesson
3:37
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
3:54
that is a keyword static which name is same as that of the file name here so
3:59
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
4:09
demo static and instantiated with the constructor and the new keyword so here
4:15
we're having this ST1 dot increase ST1 dot increase so two times the static
4:21
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
4:31
one instance of the static variable will be defined here only one instance of the
4:36
static variable so this static word will be defined once only irrespective of the number of objects we are going to define under this
4:45
class that is the static demo as a result of that ST1 when it is increasing the
4:51
static bar by one then same same increase function will when will be called
4:57
from the ST2 it will be oh
#Computer Education
#Java (Programming Language)