Java Programming Tutorial - 44 - Enumeration

Sharing buttons:

sup guys Bucky welcome to your 44th Jeff

tutorial and in this tutorial I'm going

to be going over something called

enumerations or enum we learned about

them before I think we might not have if

we didn't you know no big deal we're

both learning today but the first thing

that we're going to do well let me tell

you what enumerations are they're pretty

much kind of like classes but they

you're going to use them to declare

constants at least in this tutorial you

are so let's go ahead and anytime you

want to do this instead of public class

tuna you put public enum DNA um and now

you can use this in a specific way so

like I said the first thing we're going

to want to do is declare a bunch of

constants of the enum type so let's go

ahead and make some constants or these

are pretty much going to be like

variables that never change so I'm going

to make like three names Bucky and these

are in essence objects so let me give

these a few parameters and then you'll

see why later on Bucky let's give the

first parameter a description word of

the object and I'm going to put nice

since you know I'm doing this story

might as well say something good about

myself and then let's just go ahead and

put a string about like um how old they

are or something I'll make it

interesting for the other two ones now

let's think of a next person let's think

of a will do constants of my first

girlfriends so my first one was Kelsey

and she was she was cutie it was

fifth-grade she's pretty cute and uh

let's see when I went out with her I was

ten years old and now put a comma

because we're not done with our entire

enumeration list and then the next girl

went out with was oh it wasn't really

girlfriend but uh we had thing it was

Julia she was a she was a big mistake

and I was twelve years old when I went

out with her so now we're done we got

three constants let's go ahead

and end the statement with a semicolon

now what we're going to do is since we

just made objects and again each

constant is an object and it's going to

have its own set of variables in this

entire enumeration so let's go ahead and

give it two variables that we're going

to use later drive it final since we

don't want to change string since this

we've just made since we're in quotation

marks and put DSC for description and

we'll copy this entire thing copy paste

and we have description right here and

what's good name for this year or

something year so now we have three

objects which are actually enumerations

or column or constants and they each

have a set of their own variables one

for description one for year so now what

can we do what do we usually want to do

when we create these well the first

thing that we're going to want to do is

create an enumeration constructor so

let's go ahead and do that by of course

typing the name of your class and then

you probably want to play your two

variables and put string description and

make sure since this is um we're going

to use this make sure this is different

than this and you should know why and

then just put string and let's put

birthday or something it isn't their

birthday but I don't want to get

confused at all I can't put yr something

because they'll just confuse me you know

I'm easily confused got something to say

about didn't think so

so now let's put DSC or whatever you

declare it up here just put equal do

description statement and then go ahead

and put birthday year evil birthday and

now what do we got anytime here's what's

going to happen with Bucky is going to

say all right with the object Bucky give

it two variables a description in a year

for the description pass in nice

in this 22 we're going to see his

birthday so set your equal to birthday

so now we can work with DSC in year good

enough but now that we have a

constructor we just pretty much set

these variables equal to where they need

to be we have no way of getting the

variables yet so let's go ahead and

create two methods that we can get the

variables so public string so this is

going to return a string and put get

description or get deaths if you're lazy

which I am so it works out and then in

this one we want to return the des see

that's just going to return a string of

the description easy enough and guess

where we're going to do in this method

public string get output get year seems

like a good name and this one is just

going to return year loan from of this

it's a little too easy so now this is

all we need to do with enumerations

again enumerations are constants that

are also objects pretty much and so we

needed two variables to represent the

two arguments and then once we have

those two variables we can build a

constructor and then this constructor

allows us pretty much to set these

variables equal to each other and return

the information we wanted so let's go

ahead and well let's just go ahead and

use the information right now so what we

want to do pretty much in our main class

right here with our main method is build

and enhance the for loop so let's go do

that right here for and the first

parameter you need is of course the

class name in the variable that you want

to use in your for loop like we use um

like description right here this is just

variable that we just made up this is

going to be the variable that we just

made up right here and that's the first

like it's pretty much what you want to

call your objects when you loop through

them the next argument is you need an

array that you need to loop through and

tell you a little bit about this because

this might be kind of confusing

anytime you make an enumeration like

this Java automatically makes it takes

this constant these constants and makes

a built-in array and the array is called

whatever your enumeration is called

instance on a my toon it's called tuna

and when you write values like that this

is a built-in an array of the constants

right here

so for saying are a um I need an array

BAM you got an array right there and

again you can't change this keyword

values this values is called a static

method in again this is how you get a

built-in RA so this is going to loop

through each of the constants first it's

going to loop through Bucky and Kelsey

then Julia and it's going to assign them

to people well you'll see what does so

now we just need to make a print

statement so let's go ahead and put

system and why do we need to make a

print statement so we can actually see

what's going on

system.out.print out and let's just go

ahead and make our two arguments the

format is going to be percent s and

let's put a tab add some spacing % s @ @

ab % s and a newline so it's going to

write our name our description our age

and then it's going to go to the next

person so now we need our arguments the

first thing we want is people because

that's what the object is pretty much so

in the first one is just going to loop

through our name which is this Bucky

in our next argument what we need is to

take that people object and use the

method get' why would I name that method

people dot get description and what that

is going to do is it's going to return a

arm you find my screen recorder see how

much time I'm at

I got minute 30 what's going to do is

return a string of our description and

this last one people dot get

year and what this is going to do is

first find you to get it messed it up

for me people to get ear and what this

is going to do is return a string of the

year so let's go ahead and make sure I

have a semicolon at the end of that and

once again and make sure I have this and

let's go ahead and run it so run this

okay and look at my results I got the

object the description and the year

object description your object scription

yer so what it did again see if I can

get it was it loop through the entire

array of people and for the first

argument gave us the object name

description name and the year whatever

year I signed will so in the next

tutorial we're going to be going over

this more but thank you for watching

don't forget to subscribe and I will see

you next tutorial