declare

Java Enum - Make Your Own Enum



Sharing buttons:

today we're learning about enums the

word enum that makes no sense to me what

kind of word is enum I like Eminem

seriously if you're having trouble on

enums you have stumbled upon the right

video because we are going to walk

through together exactly how to use

enums and get it working in your program

welcome if you're new here my name is

Alex I post a Java video on this channel

every single week for you so if you're

new here and you might be interested in

seeing that then please consider

subscribing so let's start getting your

enum working by going to file new Java

project I'll call it something like

younam's are fun in a lot of videos I

type kind of are fun at the end because

if you type the exact thing like if you

type enum that'll screw with the

computer so you don't want to name it

Ino I'll call it something a little

different a little variation and then

inside of that we're gonna make a new

class and call it let's do a nose add

the main method so we can run our code

and we're all set up I'll make this a

little bigger for you an enum is

basically if a class and an array had a

baby it's a list of values that doesn't

change but it's set up like a class and

you access it like a class so refresh

around erase if you had an array of size

strings and you had like levels of a

game you recall it like levels and then

you would put what's inside so we have

level low medium and high and then to

access each of those levels you would do

levels dots or levels of zero put a

static keyword in front of it so you

don't have to make an object to access

it one two and three boom so easy just

passing in the index and then printing

out the elements of the array so now

let's turn this array into this weird

enum thing and enum can be put inside of

a class or outside of a class so let's

start with outside of the class why not

let's make an enum

you type enum it'll turn purple and that

just tells us that the computer knows

what it does next let's name our enum

and we'll do levels just like our array

now inside of here is where we list our

levels so we've got low just like you

said Alex we've got low medium and high

now there are red underlines everywhere

why this is how you make an enum why are

there so many red underlines let's look

at the message and it's just like dude

what is going on what are you trying to

do well instead of like how levels here

is strings and an array has different

types in ohms do not have types it's

just a list of like labels and you might

be like well why would we ever need that

but I'll show you in a second so instead

of doing strings up here in the double

quotes we're gonna type out some labels

and see if that fixes things

yeah and they go away the convention is

to always do these in uppercase

just because it's kind of easier to

recognize if it's an enum rather than a

variable you can you can use lowercase

if you want to but it's just good

practice to do uppercase so you know

that it's not like a variable or

something now how do we access these and

if they're not string variables then

like what are they well let's try to

print one out and and see what happens

and also why would you ever need this

I'm gonna get to that in a second so

together let's go into our print

statement and type levels because that's

the name of our enum up here and we'll

do a dot and this will bring up high low

and medium everything inside of the ena

will click one like high for example and

see what gets printed out and it's

actually the name of the value even

though it's not a string it prints out

the name

we type the name of our enum levels l

equals levels dots low or little is that

high I'll do your low this looks really

weird to me having a lowercase e no more

class so convention is to type it up

here and I'm going to do one level level

okay and now we can just type out l know

L and now this should be low cool

now why would we use an enum we use

enums for things that do not change

rather than have an array of strings low

medium high

it's cleaner and easier to do with an e

note because you don't have to go into

the string array and then pass an index

and then say if that string is equal to

low it's kind of a lot so enums are just

cleaner and we can run different code

based on different values of the e know

but let's say we have a video game and

the user selects a level and say we took

input from the computer they typed in

low and we're supposed to set the level

too low well let's just pretend that L

is that level they enter and we want to

setup different levels so we can throw

this inside of a switch case the switch

case is basically a glorified if-else

setup is if-else statements and we'll

say if it is equal to low then we'll

print out low level break and the same

for high a medium if L is high or medium

it will print medium your tie will print

hi

we save this and run it since L is low

it'll print out low level you can get

rid of this print statement so that's

basically how you use an enum let's do

another example so that you know exactly

what's going on and how to do in ohms

and now let's do it inside of the class

because you can make an enum outside or

inside so let's say we have a list that

doesn't change well we know that enums

are often used and are really good at

storing a list of things that doesn't

change so to make an enum you type enum

and then the name so let's say we have

flavors of ice cream we'll call it

flavor inside of here instead of doing

like strings and double quotes or any

types we can just write out what the

flavors are like say chocolate we're

doing it in all caps because it helps us

keep track and know that it's not a

variable it's a little different

we'll have vanilla and I'll have

strawberry we can access the elements in

the enum by creating that enum object

the name is flavor guess who name is

something like Flavor Flav is new flavor

aha but it's not it's not an object

it's an enum so we have to just say

flavor dots chocolate or flavor ego

strawberry or flavor definitely let's do

vanilla cuz that's my favorite now we

can toggle code based on what the flavor

is so last time use a switch case now

we'll do something like an if statement

we can say if Flav is equal to vanilla

we have to do slave dot vanilla or

flavored vanilla like this each would

work but we'll do flavor if it's that

then we'll print out its vanilla and

then do the same

for the others chocolate chocolate

strawberry strawberry okay now if we

save and run it it works the exact same

way even though the enum is now inside

the class so again an enum is basically

a list of things that do not change and

they're super good for things like

levels like sizes so if your teacher

asks you to use an enum and you're like

well why can't I use something else the

answer is you can but they just want you

to practice this so just write this

right what you need to in here list them

out and then run different code based on

what value of the enum is what enum

stands for enumerated enumeration I

think and you can also have methods

inside of your email so you could have

something in here a method called like

get vanilla or something get vanilla and

then we can just get the flavor flavor

dot vanilla so you can have both methods

and these constants inside of you know

and then if we ignore it everything and

ran flav dots get vanilla get vanilla

method now and we have that print it out

but most of the time free nouns are just

gonna have this little list here and

then run code based on what it is so I

hope this helped you out with in ohms of

Java please leave a like if it helped

you and I'll see you in the next video

have a great day

[Music]