What's up guys, it's Shubham and welcome to
the seventh video lesson of Java tutorial
series for beginners. In this video we will
be talking about different types of variables.
So, in Java, we basically have three types
of variables. The first one is local variables,
and the second one is instance variables.
And in the third one is the static variables.
So let's discuss all the types of variables
one by one. Let's start with local variables.
A variable which is declared within a method
or constructor or block. those variables are
known as local variables. So here I've mentioned
constructor, I haven't covered this topic
yet. So as of now just assume constructor
as a method, okay. So it's basically a method
with the same name as a class. So suppose
if inside a class you declare a method, with
the same name as of class, then that is a
constructor. To as of now just assume this
as a method. I will discuss about constructors
in more details in my upcoming videos when
I will discuss about oops concept, now, let's
discuss about the scope of local variables,
the scope is limited to the block or method
within which the local variables are declared
means that if suppose inside a method, you
have declared a variable, then you can only
access those variables inside that method
only you cannot access those local variables
from outside of that method. Right. The next
point is the local variables created when
the method is called and destroyed once the
call returns from that method inside which
the local variables are declared, okay, and
the last point which I want to mention here
is that the initialization of local variables
is mandatory. Suppose you have declared a
variable inside a method and you haven't initialized
it and just execute the program. Then you
will probably get a compilation error that
you haven't initialized here variable You
must have to initialize your local variable
in order to successfully compile and run your
Java program. So let's see all these with
examples. So let's go to our Visual Studio
code. So here I am inside my Visual Studio
code. So as you can see, this is our basic
Java program. So inside our main method, I
have declared a variable, as I have declared
this variable inside a method. So this is
our local variable. So that's how we are declaring
a local variable. And after declaring we are
initializing our variable, so initializing
means storing the value for the first time.
So I've initialized a value of 10 inside this
local variable, and then I'm printing this
variable. Let's check by compiling it. Great
is successfully compiled, or let's execute
the byte code So, as you can see, it prints
10. Now let's disable this initialization
part. As I've already mentioned that if you
will not initialize your local variable, then
it will give you a compilation error. So it
is mandatory to initialize your local variable.
Let's comment out this as of now and let's
try to compile our file again and you will
get an error that local variable is not initialized,
so you must have to initialize your local
variable. Okay. Now let's see the example
for scope of local variables. Let me clear
my screen first. And as you can see, I have
created a new method called sum. And inside
this method, I have declared two variables
a and b. And these are local variables as
I have declared this inside the sum method.
And as you can see, you can also declare multiple
variables in the same line as I do Here by
separating them with a coma okay and after
that we are just printing the sum of these
two variables a and b as we are not returning
any value from this method that's why we have
added the return type as void. So, we will
see all these later when we will discuss about
the methods in my upcoming video. So as of
now just notice that it will return nothing
so I've used quite now if I try to access
these variables from main method, suppose
I will write inside print dot out dot print
ln and I will write a okay and save this file
and try to compile our file and then you will
get an error that cannot find symbol a, okay?
That means a is not accessible from main method
and that's because it is a local variable
and the scope is limited to this method only.
But these variables can only be accessed within
this sum method. And it cannot be accessed
from outside of this method. Okay. Now let's
call this method and see how the variables
are created and destroyed. Okay. So, let's
delete these lines. Now, as you can see this
sum is not static function, you must have
to create an instance or object of this variables
class. So, whenever I tell instance or object
to both refers to the same thing, instance
and objects are same. So, we will discuss
about these later when I will start oops concept
as of now, just know that instance and objects
are same. So, whenever I use the instance
or object word, but don't get confused. To
access this somehow, we have to create an
instance or object of this class to suppose
if I don't create an instance and just try
to directly call this method from here and
Try to compile this file and you will get
an error that non static methods some cannot
be referenced from a static content Okay.
That means the sum is non static. So, you
have to create the instance of this class
and then through the instance you can call
this sum method. How can we do that, so, let's
see, we will write the class name followed
by the name of the instance whatever you write,
I will write OBJ okay for my reference and
I will write new keyword and followed by the
variables and see this is the this is basically
a constructor. So, I am creating a new instance
of variable class and through this OBJ I will
call some okay now, let's save this and let's
execute this You will see 20 is printed. So,
what happened here is when you call this method
to this object of variables class method some
is called and when the sum is called it created
the two variables a and b as they are declared
inside this summer. Okay. And in the second
line, the Sum of a and b are printed. And
as the call is returned from this function,
then the variables get destroyed. But that's
how it works. So that's all for this video
guys. We will see the other types of the variables
in the upcoming videos. If this video was
helpful, then please hit the like button and
subscribe to my YouTube channel if you're
new, and don't forget to press the bell icon
so that you will get the notifications of
my latest video uploads. See you guys in the
next video till then take care and stay safe.
