0
why I use get in java and return void value?
before I set value, I use two clasess
1 Answer
+ 2
You mean why using setters and getters in a class?
A setter changes a value. For example you have the class Cat and with a String color. You could change the color like this. Cat.color = purple
But with a setter it is not possible to change color directly. In a setter you can define which changes are allowed and what is to do with exceptions.
And a getter returns the value. In this example it would return the color of Cat.
https://www.sololearn.com/learn/Java/2154/?ref=app
see also encapsulation:
https://www.sololearn.com/learn/Java/2162/?ref=app