Getters and Setters - Learn Getters and Setters in Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everybody we're doing something a little different today we're gonna be doing getters and setters together so if you need to make getters and setters for your projects and are a little confused and don't worry because I'm gonna walk you through exactly how to do it and it'll be a great time if you're new here my name is Alex I make Java tutorials on this channel just like this one every single week so if you might be interested in seeing that then please consider subscribing so let's start learning about getters and setters by going to file new Java project I'll just call I like getters in setters inside here we'll make a new class and call it like yay getters and setters and I should have added a main method but we could just type that back in okay so getters and setters getters and setters basically let you get and set variables inside of a class a class represents an object since Java is object oriented an object in the real world has attributes and things that can do so in our class we represent an object in the real world inside of a computer by just typing attributes and things they can do we type attributes with variables like string name and int age and we represent things that can do with methods like say happy birthday and then pass in a name or something set it up right and now this object knows a name and age and you can say happy birthday with it so I can print out an object yay getters and setters when I fix such a long name and we could just say a dot say happy birthday to Alex it's not actually our birthday that's just an example what our getters and setters these variables up here name and age can be sets like this in order to do cool things like say happy birthday we needed to use this name which is actually up here all of these attributes or variables inside the class are being used and interacted with all the time with our methods but weird things can happen especially if the variable up here name is the same as a parameter down here things can get confusing the for the computer and it might not know exactly what you're trying to do I'm going to rename this file to account instead of this really long name I'm going to call it account and replace that here and here and delete this and we'll start over instead of reading the file manually a lot of program editors will let you hover over it and do rename file to account dollar and that's what we want to do so say we have this account object this happens all the time with like big software companies and even just small businesses and everything will have an account with information on you like your name date of birth age things like that so for this example we have name and age say our uncle Pablo is opening an account and we want to make an account for Pablo well we have an account object here and let's start making one for Pablo right now if we do a dots the name of the object and then a period will bring up everything that that object can do it has these variables here called age and name which we have here but it also has all these other methods and these are all defaults but we're not going to worry about those we could do a dot age and try to set it that way we can say Pablo's age is 34 and we could do his name is Pablo public and then we can print out a little shortcut here if you don't like typing system dot out dot print line in parentheses as well you can type this out and then control space and I'll finish for you now we could print out his accounts age and his accounts name and we get 34 in Pablo and that is great and this is one way you can do it it works we have his account and the information is inside there so if we had a method like print details and then we print it out name comma age and then instead of doing these we could just call a dot print details if we did that it would work but if you have multiple accounts say we wanted to create three different accounts for three different people we would have to do this again a dot H equals three for a dot name equals whatever the name is and whatever their age is actually so all this setting and stuff can cause problems especially if you don't have the exact equals number equals name basically it's bad practice to do it this way setting the variable like this we want to make methods that does it for us we want to be able to set the age and set the name and then also if we want those variables we can do getting the age and get the name instead of dealing with the variables themselves and that is what getters and setters do so let's instead access these through getters and setters instead of the variables themselves we'll start by making a set method or a setter by just doing public void set name and then pass in a name string name now we're just going to do name equals name this name equals this name but this can really confuse the computer because it doesn't know what you're talking about are you trying to set the parameter equal to this or we try and set this equal to the parameter it doesn't know because the variable name is the same so what we have to do is type this dot name this tells it it's this accounts name not the parameter I have more detailed video on this that this keyword in Java it's up on the screen now if you want to learn more about this and not be confused so I have set name now we can do the account dot set name you know set it to Pablo now if we run it it will print the details and we've got Pablo and he's currently 0 because we haven't set the age yet so we'll just make a setter for the age set age and we'll pass in the age we want see this dot age equal age this is all that setters do they just set the variable to whatever you pass in as a parameter the reason we're doing public void and no static keyword is because these will be accessed through the class so we don't need static so how we're doing a dot set name that's fine since we're doing it through account a specifically but if the static keyword was there we would not have to do a dot we could just do set name we will set his name here a dot set H we'll set his age here he's 34 print details and yeah table 34 we can also get and store that as a variable which is also super useful so let's make getters setters set within the parameter to the variable getters just get the variable so we can return this right here we'll put the return type will do name so my name is a string so we will do public string gets name then we'll return name you could also do this dot name just to be more specific but it'll work either way I like doing this net dot name because it says exactly its accounts name we'll do the same for get age public since we were turning an age this age is an integer get H return this H cool now inside of our main method we could do print out a dot get age and then print out a dot get name run it we print out Pablo here we print out 34 and then in the print details method we print these details it's okay to use the variable names inside of the class but theoretically you could also do get name here and get age and this is a lot less confusing in the long run you don't have to set the variables themselves through the equal sign you can just call the methods and you'll know that everything is handled correctly in the background and for whatever reason if it doesn't work you can go to that class and fix it so basically this reads more like English whereas the setting with the equal sign a dot name equals Pablo kind of takes a little more effort so now that we've built this together I'm going to explain to you everything step by step on what's going on so I'll just make this full screen when we click the green run-button we go into the main method the first piece of code here is to create an account called a it knows what an account is because we have this account object here that has these properties name and age and these methods set names set age get name get age and print details now we have the object a if we do a dot it brings up a list of all the attributes and methods here in here that I just named and we picked one we want to set the name Pablo and so what that does is it goes in to set name this variable up here is equal to the parameter passed in here so now name is Pablo next we set the age we go in here this age is 34 next we print out get name that goes in to get name and we return this name so since we set it to Pablo it returns it in this return statement and thus ends Pablo back into this print statement so that's why we print Pablo next we get age go in here return this age which has been set to 34 already sends that with this return type it's an integer send that back into where we called it and we print 34 lastly we call the print details method inside the account and we print the name and the age separated by a comma and so that is why it prints out like this we can change this to Ricky and maybe Ricky is 15 and it changes everything so this makes it really easy and like English II to modify objects and variables so I hope this helped you out with getters and setters there really not too bad Oh bonus tip bonus tip yes bonus tip bonus tip this is really really cool okay so you don't have to type all this you can there's a feature in Eclipse and I think most other editors that will let you generate them so this was mind-blowing when my teacher did this but basically you right-click anywhere in your class and then one of the settings here is a refractor no source yeah source generate getters and setters so click that and some crazy crafts gonna happen we want to generate getters and setters for age and name so that had generate and now inside that class it generated right here it creates getters and setters just like what we did it doesn't have this here because you don't need it I just kind of added that but it's the exact same we return the name as a string we set the name with string here I get the age yeah we turn age as an integer set age with this parameter and yeah you could do generate getters and setters instead of having to write them yourself so I hope this video was helpful on getting you set up with getters and setters thank you for letting me teach you have a great rest of your day and I'll see you in the next video [Music]
Info
Channel: Alex Lee
Views: 222,138
Rating: 4.9408779 out of 5
Keywords: getters and setters java, getters and setters in java, getters and setters tutorial, getters setters java, java getters and setters, java getters and setters example, getters and setters, setter method java, java getter and setter, getters and setter java, setters and getters java, setters and getters, setters and getters in java
Id: 6wVmqY-CrGM
Channel Id: undefined
Length: 14min 12sec (852 seconds)
Published: Thu Nov 21 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.