declare

016 - Global Variables - Java



Sharing buttons:

what I wanted to do is take a minute to

go through what a global variable is and

and how it works in Java

now all programming languages are a

little bit different with respect to the

global variables in a previous video I

kind of showed how you would do it

inside of a c application or C++

application and basically if we take a

look at what we've got here in this C

here and this this is our package name

so we have a package and let me change

that so it's not quite so dramatic so

we'll clear this so here is our package

name that we had declared earlier and

this is our our main class now the by

main what I mean is this is the class

that we call that has our main function

in it so this is the the entry point for

the application and Java there's a few

different ways that you can do a set of

global variables one you can't do like

in C++ or and or in C I could come up

here outside of any of my my functions

and declare a variable and it's

automatically global because it doesn't

have scope inside of the actual variable

I'm sorry inside of the class or inside

of a function and therefore it's it's

just naturally going to be global now

despite what a lot of people tell you

that you know you can't have global

variables they're evil and they're going

to cause the world to collapse that's

not entirely true and mostly said by

people that don't write a lot of

applications because in reality there's

a lot of situations that come up where

you do need to have global variables now

in Java it's a little bit different

because I can't have code in my java

application that is outside of a class

it's kind of it's actually kind of cool

because some of the problems that people

have with global variables Java kind of

solves because

we don't actually have global variables

that are not related to some class

somewhere now the easiest way to do a

global variable is to actually declare

it as a static variable of some class

now in this particular instance I have

come in here and actually made this

global variable part of my main class 1

class so you'll see here I have a public

variable I'm saying that I want to call

it from anywhere I want it static and

earlier we had talked about static but I

didn't get into a lot of explanation on

it but when you declare something static

you're basically saying that this in

this case a variable in this case an

actual function I want only one copy of

this thing to exist in the entire

application I'm basically saying there's

going to be one static location that we

store this particularly in this case and

I don't want any other copies of it now

if you think about a situation where we

have for example if we go over here and

take a look at our class to Java we

don't have static here and what that

means is that when I declare an instance

of this this particular class this class

to class I am going to end up with us

with a different copy of this function

for every instance that we create when I

declare it as static I'm only going to

have one instance of this dysfunction or

that in this case the variable no matter

how many instances I declare so in our

main class I have just come in here and

told it that I want a public static it's

going to be an integer this is going to

be its name and then we're going to go

ahead and initialize it to 3 now I don't

have to do this but I went ahead and

initialized it now what this means is

that in the case of what we were doing

in the in a previous video we actually

came down here and created an instance

of class 2 so our class 2 we called it C

2 we create a new class then we access

the the fun

inside of that class and then we hear

we're actually accessing a variable now

we're doing it based on the instance of

that particular class now because I

declared this static I don't I don't

need an instance of the class to get to

this variable all I need is the class

name so the class name in this case is

class one so if we come over here and

look at where we actually are using that

particular variable let me clear this

you'll see that right here I am

accessing class 1 dot global J equals 22

and then here I'm accessing it and

telling it to print it so all I've got

to do in this situation where I have

this global variable is to give it the

name of the class not an instance of the

class but the actual name of the class

to be able to get to that variable and

use it now some recommendations that

I've seen floating around recommend that

you actually create a separate class

that holds all your global variables in

it you can do that if you want to it's

not entirely necessary if you declare

that the main thing you want to do is

declare all your global variables in a

place that everything has access to it

and put everything in the same place in

other words I don't want to declare a

global variable here and then come over

here and declare another global variable

over here when you start doing that it

gets a little confusing trying to keep

up with what all your global variables

are it doesn't hurt anything because the

global variable I'm going to be

accessing from over here is going to

have class two on the front of it

instead of class one but it just makes

it easier for your source code if you

declare all your global variables in one

spot in application whether you create a

separate class just to be global

variables which makes it honestly easier

probably to keep up with because I could

create a class called global VARs and

let that be where I put all my global

variables and all I've got to do is

declare them static and then whatever

they are and then I can get to them from

anywhere

in the application so if you're using

objective-c or you know any of the other

programming languages there's some

concept of a global variable and in fact

in Objective C that it should work

pretty close to the way that I'm

describing here in Java so you can

declare the the same types of static

variables I'm pretty sure I'll have to

look at that and I'll actually do a

video on objective-c

here in the next day or two to show how

you actually do this kind of thing in

Objective C but in all programming

languages there's the concept of global

variables and and you just want to make

sure that you organize them keep them

named something that you can actually

keep up with and I usually put global in

front of all of them you also could make

them all uppercase although I don't

really like that because that that

generally is just forced constants

instead of you know just variables that

we could change but in any case this is

how you deal with with global variables

inside of Java