‫Instructor: Hi, within this lecture,
‫we're going to learn about getters and setters.
‫So when we deal with classes,
‫we generally deal with constructors
‫and we also deal with concepts called getters and setters.
‫So what does getters and setters do?
‫So let me change this address to private again,
‫like we did in the previous lecture.
‫And remember, when we did that,
‫we couldn't reach homer.age here,
‫not for changing and not for reading as well.
‫As you can see, homer.age is not reachable,
‫not for printing out here as well.
‫Maybe if we want to print out this,
‫maybe we want to read the value
‫but maybe we don't want anyone to change this value.
‫That's what getters and setters do.
‫When we define getters,
‫we actually can provide tools
‫in order to read those values, get those values.
‫And if we provide setters,
‫then someone, some developer can actually change the value
‫of those attributes as well.
‫In order to create getters and setters,
‫you can right click and say Generate one more time,
‫and choose getters and setters,
‫and then choose everything from that list.
‫And click OK.
‫And as you can see, it creates some methods.
‫So if you look at the methods closely, it's fairly easy.
‫You can create it on your own.
‫It says that public String,
‫so it's returning a string.
‫And in this case, it's returning name.
‫Even though it's private,
‫we can reach it from here
‫because we are in the same class, right?
‫So it's fairly easy.
‫We are getting the name and we are setting the name.
‫So as long as these getters and setters are public,
‫we can actually reach those methods
‫from MainActivity as well.
‫So if we don't make them private,
‫then we can reach those.
‫And it doesn't make sense to make getters
‫and setters private, okay?
‫And if you come over here,
‫we're still getting an error
‫because we cannot reach name from the MainActivity
‫since it's private, but we can reach getName method, right?
‫So rather than printing out this,
‫I'm gonna print out the homer.getName.
‫So as you can see, now we see the methods as well,
‫like getJob, getAge, getName.
‫Now if I do that, I won't get any error.
‫And if I run this, now I can reach the Home name
‫and I can print it out.
‫So maybe you want somebody to read those values
‫but you don't want anything, anyone
‫to change those values.
‫For example, I can actually change the name
‫of the Homer to like Homer Simpson
‫with setName method.
‫And if I print out one more time
‫by using homer.getName,
‫I can see Homer and then Homer Simpson consecutively
‫in my Logcat like this.
‫So if I don't want anyone to change the name
‫once they have initialized it,
‫then I can delete these setters.
‫I can only have the getters over here
‫or I can do vice versa for some reason.
‫Okay, so this is what getters and setters are for.
‫So if you see getters and setters, don't be surprised.
‫They're not complicated.
‫They're actually very, very easy but they are useful tools.
‫So when you deal with a class,
‫when you deal with a model,
‫if you create some attributes,
‫if you create some constructors,
‫make sure you think that if you need setters or getters.
‫If you need those, then just import them,
‫like generate them and use them in a way that you want.
‫If you don't need those, then of course,
‫you're more than welcome to ignore it as well.
‫And remember, we're printing out everything in this case
‫but in a real-life example,
‫we would just display them on the emulator,
‫on the user interface as well.
‫Now we're gonna stop here
‫and we're gonna have some challenges
‫and assignment in the next lecture.