0

About java

I could not understand the use of getters and setters in java could anyone say how to get better at these things.

30th Sep 2024, 7:25 PM
Lord Robertson Kundu
Lord Robertson Kundu - avatar
3 Respostas
+ 2
Hello Lord Robertson Kundu getter and setter are access methods that you can use to query or change values. Suppose you have a house. You don't want people to come in through the window or the chimney. Of course you want them to ring the doorbell first and use the door after you have decided whether they are allowed in. That's basically how it works with classes. The user should not be allowed to query and change any values, but should use getter and setter for the values that you think should be changed or queried. And of course you can use it to specify how values may be changed. Suppose you have a class of players. Each player has a name, an age and a score. First of all, there is nothing to prevent other classes from querying these three values. But you want to prevent negative values from being entered for the age. And the score may only be changed under very specific conditions. Perhaps not the best example, but I hope I was able to give you a better understanding of the idea of getter and setter.
30th Sep 2024, 8:03 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Lord Robertson Kundu practice practice practice is the best way to get better / improve in coding in general... https://sololearn.com/compiler-playground/coMLfYEI4VxY/?ref=app
30th Sep 2024, 8:04 PM
BroFar
BroFar - avatar
0
please can anyone say this
30th Sep 2024, 7:26 PM
Lord Robertson Kundu
Lord Robertson Kundu - avatar