Getters-Setters And This Keyword - Part 2

Complete Step By Step Java for Testers Classes And Methods -> Object Oriented Concepts
9 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€85.87
List Price:  €120.23
You save:  €34.35
£74.39
List Price:  £104.15
You save:  £29.76
CA$138.18
List Price:  CA$193.46
You save:  CA$55.28
A$153.39
List Price:  A$214.75
You save:  A$61.36
S$128.92
List Price:  S$180.49
You save:  S$51.57
HK$779.80
List Price:  HK$1,091.76
You save:  HK$311.95
CHF 80.49
List Price:  CHF 112.70
You save:  CHF 32.20
NOK kr1,009.67
List Price:  NOK kr1,413.58
You save:  NOK kr403.91
DKK kr641.01
List Price:  DKK kr897.45
You save:  DKK kr256.43
NZ$170.65
List Price:  NZ$238.92
You save:  NZ$68.26
د.إ367.21
List Price:  د.إ514.11
You save:  د.إ146.90
৳12,159.63
List Price:  ৳17,023.97
You save:  ৳4,864.33
₹8,813.74
List Price:  ₹12,339.59
You save:  ₹3,525.84
RM422.70
List Price:  RM591.80
You save:  RM169.10
₦152,684.73
List Price:  ₦213,764.73
You save:  ₦61,080
₨28,172.18
List Price:  ₨39,442.18
You save:  ₨11,270
฿3,233.20
List Price:  ฿4,526.62
You save:  ฿1,293.41
₺4,116.73
List Price:  ₺5,763.58
You save:  ₺1,646.85
B$546.03
List Price:  B$764.47
You save:  B$218.43
R1,779.46
List Price:  R2,491.32
You save:  R711.85
Лв167.77
List Price:  Лв234.89
You save:  Лв67.11
₩139,375.48
List Price:  ₩195,131.24
You save:  ₩55,755.76
₪336.41
List Price:  ₪470.99
You save:  ₪134.57
₱5,711.89
List Price:  ₱7,996.87
You save:  ₱2,284.98
¥14,830.67
List Price:  ¥20,763.54
You save:  ¥5,932.86
MX$1,876.23
List Price:  MX$2,626.81
You save:  MX$750.57
QR364.02
List Price:  QR509.64
You save:  QR145.62
P1,342.46
List Price:  P1,879.50
You save:  P537.04
KSh12,918.70
List Price:  KSh18,086.70
You save:  KSh5,168
E£4,854.20
List Price:  E£6,796.08
You save:  E£1,941.87
ብር14,292.55
List Price:  ብር20,010.15
You save:  ብር5,717.59
Kz91,186.39
List Price:  Kz127,664.59
You save:  Kz36,478.20
CLP$96,831.31
List Price:  CLP$135,567.71
You save:  CLP$38,736.40
CN¥713
List Price:  CN¥998.24
You save:  CN¥285.23
RD$6,296.92
List Price:  RD$8,815.94
You save:  RD$2,519.02
DA12,990.59
List Price:  DA18,187.34
You save:  DA5,196.75
FJ$226.12
List Price:  FJ$316.58
You save:  FJ$90.46
Q765.93
List Price:  Q1,072.34
You save:  Q306.40
GY$20,890.84
List Price:  GY$29,248.01
You save:  GY$8,357.17
ISK kr12,333.76
List Price:  ISK kr17,267.76
You save:  ISK kr4,934
DH908.31
List Price:  DH1,271.67
You save:  DH363.36
L1,670.58
List Price:  L2,338.87
You save:  L668.29
ден5,277.34
List Price:  ден7,388.49
You save:  ден2,111.14
MOP$802.43
List Price:  MOP$1,123.43
You save:  MOP$321
N$1,761.48
List Price:  N$2,466.14
You save:  N$704.66
C$3,674.73
List Price:  C$5,144.77
You save:  C$1,470.03
रु14,082.30
List Price:  रु19,715.79
You save:  रु5,633.48
S/353.21
List Price:  S/494.51
You save:  S/141.30
K416.61
List Price:  K583.27
You save:  K166.66
SAR375.17
List Price:  SAR525.25
You save:  SAR150.08
ZK2,374.72
List Price:  ZK3,324.71
You save:  ZK949.98
L436.04
List Price:  L610.48
You save:  L174.43
Kč2,099.65
List Price:  Kč2,939.59
You save:  Kč839.94
Ft33,821.21
List Price:  Ft47,351.04
You save:  Ft13,529.83
SEK kr945.93
List Price:  SEK kr1,324.34
You save:  SEK kr378.41
ARS$136,092.68
List Price:  ARS$190,535.20
You save:  ARS$54,442.52
Bs690.01
List Price:  Bs966.04
You save:  Bs276.03
COP$400,414.31
List Price:  COP$560,596.05
You save:  COP$160,181.74
₡50,481.69
List Price:  ₡70,676.39
You save:  ₡20,194.69
L2,617.73
List Price:  L3,664.93
You save:  L1,047.20
₲721,217.97
List Price:  ₲1,009,734.02
You save:  ₲288,516.04
$U4,006.62
List Price:  $U5,609.43
You save:  $U1,602.81
zł365.33
List Price:  zł511.47
You save:  zł146.14
Already have an account? Log In

Transcript

Hello all welcome back to the next one. So let's start from where we left talk about the getters and setters. And we were talking about the car and the object, the car class and the object. So we created one object called BMW we set the value of make, and we get the value of make as well. We created all the getters and setters, we created by hand manually also by typing and we created them by using Eclipse shortcut also. So all I wanted to explain there was that do not think that getters and setters, guess getters and setters are something unique.

They are something very different concept. There are simple methods like any other method, just to make them more sounds make sense. We call them as getter and setter because one returns a value and one sets of value. And we can create them manually and we can start rate them also. So now let's let's set and get one more value so that we know or maybe you know what, let's create one more object and let's call it as Benz. And I'm gonna say new car and maybe Benz dot This time, let's set the model.

And I don't know what the model accepts. Let me just take a look. So get model and set model is string. So model is a string. I'm going to provide the model as maybe I don't know see 300 is okay. And then let's just get the model back.

So I'm gonna do this thing, Ben's dot get model and this is going to print the model out Our new object. So now we have two things to print, run as Java application. Here at the bottom, you can see the first one printed the make of BMW. The second one printed the model of events, right? So we can set and get values using our objects. And this is how we use objects in a different class.

Now, I wanted to show you this thing again. So let's talk about air. Let's make it public again. And, let's see. So if I do Ben's, now I can access it directly, right because it's public, Ben's dot here, and I can set it to maybe 2020. I'm going to future and then I can kind of try to get the value of the Let's try it out.

So see So, and bend dot get here. Right? So let's print it out. And yes, it indeed works, right? So why we still decide to not use it? Right?

Why we decide these things to create getters and setters and define them as private. So this thing in Java is known as encapsulation. The preferred way is to make all these variables as private and hide them from outside world so that only the class can see them. And other reason is that we can do much more when we use getters and setters and we can do a lot more validations we can do a lot more functionality here if he said 2020 there is no way for this to validate if it's correct or not. But now let's imagine how Not using this. And let's imagine I'm using Ben's dot set here.

I'm gonna say, Tony, Tony. Now I can go into my car class. And let me bring this up. So we have some space here. Okay, so for the set here, I'm setting this audio. Let's imagine in future there is a requirement and we have to do some more validation, then we can easily use this method and utilize it to do all these validations.

Let's say I want to do the validation of F here. So maybe I'm saying only said the error if the error is greater than 1900, something like that. Some validation and I'm just making up things right now. So which means kind of maybe valid I don't know if Ben's exist before 99 I think they existed, but I'm sure maybe the model three c 300, was not there before 1900. So maybe this is a good validation. So if here is greater than 1900, if only it's provided as greater than 1900 from the outside of the class, then only set it otherwise.

Otherwise, let's call it like this here is not valid, some validation. So don't worry about if the validation about bans is correct or not. What we are trying to understand here is yes, if we want we can do some validation, if we are using the getters and setters. So this is one great example and usage of this encapsulation like hiding these member variables from the outside world, and then using the getters and setters to use them. side of the class. Now, this, this is gonna work fine.

So let's run this as Java application. All good. But if I give it as like 1800, so which is not a bad idea according to our code, and it tells me this error is not valid, right? So that's what happens. That's what is a good thing. And we don't want to set it.

So that's one thing that we can do using this thing. And what else I think that's pretty much it. That's what we actually want to take a look. I wanted to go over why we use getters and setters and what does it mean? And one more thing? Yeah, we looked at the this keyword, we are using this keyword all the time.

And if you might have said seen this thing, let's actually for Ben's also, God said make Gonna use the set make method. I'm gonna call it B bends. So for bands the set make is bands, mercedes benz, and let's print out bands dot get make. So I want to show you this thing here. So for the car object, we are using the BMW. So for the BMW object we are using BMW such as BMW and then printing the value and it's going to print BMW for us, right?

And the car object the car class is the same class. It's not a different class, it's the same class. But when we use a different object, and again use the set make method and provide a different value and we use the same object as Ben's to get the get make thing then they're gonna return the different values like this should return BMW and they should return Benz right? So let's again print it out and see this thing. So it says BMW Benz right. So it prints the correct value for them.

So, what I wanted to say here is that this key word, what it does is it knows what is the instance of the car object. So for a particular instance, it sets the value of the member variables. And it maintains it. So for that instance, so now BMW is the instance right? These are the instance or reference whatever we want to call them. So for BMW instance, it returns me or it, it maintains all the stuff related to the BMW instance, for Ben's instance, it maintains all the stuff related to the Benz instance that we have set and it returns us the correct values.

So this is all done by this key word also. So this means it's for this part ticular instance I'm maintaining this value I'm setting up this value, it is not set for always it is only for this particular instance. Right. So we have talked about this keyboard we have talked about the objects. We have talked about the getters and setters. So I think that's pretty much enough.

Good enough. And please download these code files practice and let me know if you have any questions and I'll see in the next lectures. Thanks a lot for attending this one. Thanks.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.