Java Enums Explained in 6 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to talk all about enums in java we'll go over what they are and when you should use them and walk through some concrete examples of how you can implement them in your java programs my name is john i'm thrilled to have you here with me let's get right to it so what exactly is an enum enum is just short for enumeration an enum is used when you have something that has a predefined set of values that don't change a common example people use is like days of the week they're always going to be the exact same seven days and they're never going to change so you might want to define in your code what those seven days of the week are in a way that people just can't add more days of the week because that just wouldn't make sense so using that example let's go ahead and create that enum for days of the week the easiest way to do that in eclipse is to just go ahead and right click wherever you want that enum to be and go to new and this is how you usually would create a class but instead we're going to pick enum just like a class you can name it whatever you want but we're going to name it days of the week and hit finish and there you go you can see it looks an awful lot like a class but instead of public class days of the week it says public enum days of the week so now we have the shell of our enum but now how do we go about listing the set of valid days of the week well we can just do it right here in a comma separated list so we can just say sunday monday tuesday etc we do have to end our comma separated list with a semicolon a couple things to note here though although it's totally valid to have these days of the week enum entries as all lowercase is typically the convention to have these in all uppercase in eclipse there's actually a cool shortcut for making text uppercase and it's just ctrl shift x by the way check out this video here if you want even more awesome eclipse shortcuts so how do you go about actually using these values back in our main method we can create an object of type days of the week just like we could for any other type of object so days of the week day equals you might be thinking we'll say okay new days of the week but if we try and do that with an enum java is going to give us an error cannot instantiate the type days of the week that's one thing that's different about enums than any other typical java class because an enum is intended to be a predefined set of values you can't use the new keyword to just create new values so what you'll be doing is just using the existing values so you'll just say days of the week dot and then you have your choice of any of those days of the week to use let's say we wanted to pick thursday so now this day variable has the value of thursday and you can use this variable in your code similar to how you would use any other type of variable for example you could say if the day equals days of the week dot thursday print out yay it's almost friday and if we run our code of course since the day we created was thursday it prints out yay it's almost friday all enum classes in java actually extend from the java.lang.enum class you don't have to put extends enum or anything here java just does that automatically but because it extends the enum class it gives you some built-in functionality that's pretty cool for example if you wanted to get all of the possible values of that enum you can just say days of the week dot values this actually returns an array of all the possible days of the week so if we wanted to do something like print them all out we can just do a simple for each loop so for each days of the week call it my day so this will loop through every possible value in our days of the week enum so all we have to do is print out myday inside that loop and we can see it prints out all the possible values of our enum but there's even more really cool things you can do with your enums for this example i've created an enum of cereals we've got cap'n crunch froot loops reese's puffs and cocoa puffs real quickly though one thing i like to do with my enums is put each enum entry on a different line like this i just think it makes your enums a little bit more readable plus now there's more room for my beautiful face here in the corner just like your regular java classes your enums can have fields so what we can do is add a field here like an int let's call it level of deliciousness and we can also create a serials constructor that takes in a value for that field of deliciousness in this constructor we would just set this dot level of deliciousness to be equal to the level of deliciousness passed in now you can see we have an error for each of these that's because now in order to create each cereal enum we have to give it the level of deliciousness we can pass that in here as a parameter at each one of these declarations so let's go between 0 and 100. captain crunch would be like a 50. fruit loops i like a little more let's say that's like a 60. reese's puffs though probably my favorite let's do a straight up 100 and coco puffs about a 75 and now we can save that and back in our main code we can get any of those cereals level of deliciousness by just calling cereals dot whichever one we're interested in let's say fruit loops and say froot loops dot level of deliciousness and we can just print that out if we want and we get 60 which was the level of deliciousness of fruit loops what you'll probably want to do with these fields is make them final so that they can't be changed for each entry so why would you want to do that well if you don't have final what somebody can do they can change that value and that's probably not something you want to allow so they can say cereals dot fruit loops dot level of deliciousness equals 107 and we can see that that change actually does take effect so somebody can go in and just change froot loops to be a level of deliciousness of 107. now froot loops are good don't get me wrong but they're not 107 good so this isn't something we want to allow so to make it so that value can't be changed once it's set we just make this final and then back in our main code we can see that we get an error when we try to change that value so making this field final stops the fruit loop fanboys from being able to mess things up and you're not limited to just one field here you can have more if you like so you might also want something like final double price all you have to do is just add that as another parameter to your constructor and pass in a value for that new field in each declaration here if you liked this video or learned something please let me know by hitting the like button be sure to check out these other videos here to keep on learning i also have a full java course available in a link down in the description if you're interested oh man i spelled captain crunch wrong i am ashamed
Info
Channel: Coding with John
Views: 3,479
Rating: undefined out of 5
Keywords: java, codingwithjohn, coding with john, java beginner lesson, java enum, enum, java enumerations, enumerations, java enums, enums in java, enum in java, enumerations in java, enum java, java enumeration, enumerations java, java enum tutorial, java enums tutorial, java tutorial for beginners
Id: wq9SJb8VeyM
Channel Id: undefined
Length: 5min 58sec (358 seconds)
Published: Mon Oct 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.