declare

Learn JAVA – Session 6, Local and global variables, Guide for Java Developers



Sharing buttons:

[Music]

hi guys welcome back this is the page

from freshersworld.com welcome to our

YouTube channel on jobs and careers so

in Java

we have something on a global variable

and local variable so in every

programming language we have global

variables and local variables so let us

understand what is the snobol variable

and what is this local variable so I

have an iPhone class so now I will try

to create a variable beginning name is

equal to de peche okay so now whenever

we declare anything inside a class we

call that as a global variable so now

whenever I declare something inside a

method so now I try to declare one more

variable with the name string name is

equal to rhom Oh fine so let's see the

output let's try to run our rain class

right we will try to see what is the

output so now in this very method

I try to print name okay I'll try to

print it name okay I'll try to select

this and I will try to print name fine

so in this I try to print name okay so

you can see the color difference right

so you can see this name in blue color

and this is something in black or light

brown select I try to run this program

so let's see the output based on that we

try to decide what is happening see the

first thing I call drink okay

this is the declaration I will go to the

implementation so if you want to go to

the implementation you have to press

control and hold on the text so open

implementation I'll go to the

implementation now right this is my

implementation so what is there Ramu is

their and Ramu is getting printed so now

globally what I have declare I have

declared it as depeche but what is

getting printed in my output what is

getting printed will see again

okay yeah Rommel is getting printed but

what is there in name

Depeche why is this happening so when we

will try to create a variable inside a

method that where you will will have the

scope

only inside that method now I have

created a variable name is equal to rhom

oh so now whenever I try to print it

tries to take the immediate variable

that is there in the method so now

immediate is nothing but the name it

doesn't tries to take it from the class

it tries to take it from that method so

now this we call it as local variable so

if you declare a variable inside a

method the variable will be having this

coke only inside that method outside

that matters you will not be able to

access that name so now whatever I have

printed if the local variable now

whatever I am printing in the second

execution of my ring so I am passing hi

hello okay that is fine but still when I

go here and not printing this song I am

printing the name the name is nothing

but it is trying to take from the global

variable so now when you declare it as a

global variable it can be accessed

throughout the class when you try to

declare a variable locally inside a

method it can be accessed only inside

that method I mean to say it has this

coke only inside the method that you try

to declare so make sure whether you want

the variable to be accessed inside the

class or it has to be accessed inside

the method just decide and try to

declare it accordingly

so now here we have declared two

variables with the same name but here

whenever I try to bring this inside my

method I always try to take the

immediate variable and it tries to print

off so if I try to print this name it's

nothing but d page what should I do

right we have a keyboard something known

as this so this refers to the current

class object so now this is a keyword

which will always try to pick from the

class so now I try to use this okay this

dot name so you can can see the color

change you can see in color change it's

now blue now it's not trying to take

this variable now whenever I use this it

is trying to take the class variable so

now this is a key

which helps us to take the gun class

object so now let's try to print this

let's see what is the output okay now

can you see what is the output D page

and D page but what is there here named

Ramu this does not get printed because

we are trying to access the global

variable so if you if I take off this

this dot name what will be the output

it's nothing but the local variable that

you have declared as Rama will be coming

up so now you make sure if you want to

declare variables globally or locally if

you declare it locally you will have the

axis only inside that method so now

let's see whether this local variable

can be accessed outside this method okay

I'll just try to change this name to you

know something's some the variable name

like phone number okay I will try to

make it as phone number and I'll try to

make it as nine nine zero zero nine nine

zero zero nine okay so this is my phone

number and I see how I took off this

name so now it is trying to take it from

the global variable that's great so I

try to print phone okay okay so now you

can print the phone number so that's

fine but will I be able to access this

phone number inside this method let's

write we know no suggestions but still

if you want to type you can type the

same name no you cannot access what is

the error fault cannot be resolved to a

variable so it means so if you have

declared a variable inside a method you

have access only excited so now this

because it has local variables so if you

declare a variable in the class so now

that can be accessed throughout your

class it might be accessed by any of

your method so make sure what you want

to declare and where you want to declare

if you want to access global variables

use this so now this is a keyword which

is used to access current class objects

so now if you have two variables with

the same name and you want to access the

global one as before you know we had the

name okay I'll try to do ctrl Z you see

we had name so we have two variables

with the same name you know in the class

level we have the same variable name in

the method level also we have the same

name so if you want to access global

name use this okay now this

right if you want to access local one

don't use this so it always try to take

the local variable which is declared

inside the method when you want to

access it so whenever you use this make

sure it tries to access you the current

rather object and not which is specific

to a method please subscribe to a

YouTube channel for more Java videos

please subscribe to our precious Volcom

for more Java related jobs

thank you

[Music]