24
Lately I have read some Java books, but there is a part that makes me confused in this type of methods accessors - gettters and setters.
The point is:
I am required to write in this type of methods, for example, getName() or just have to write getName() because it makes life easier for the programmer to better serve object-oriented programming? The way to make getters or setters is a norm or convention?
If I write, for example, porName() instead of getName() the compiler does not declare a syntax error and fulfills the same function, only the method name is changed.
Gets and Sets were agreed, as well as method name start with lowercase letter and other things.
– emanuelsn
Related questions: that one and also that one.
– Luiz Vieira
@Luizvieira If this question is really about getters/setters in Java, the correct answer would be Math. Now if we are talking about software design, the answer would be much wider rs
– wryel
@wryel Truth. I only found it relevant to quote the other questions because this question also questions whether they are 'facilitators''.
– Luiz Vieira
In addition to @emanuelsn’s comment, some frameworks (or frameworks species) use this convention to do their job (JSF uses and is an option in Hibernate). In my vision this is the answer: is a convention to facilitate communication, and is a functional convention for some frameworks. The answer to the title of the question, which has nothing to do with its content, is: use getters and setters is not mandatory. We use getter to allow an attribute to be read outside the class and we use Setter to allow it to be modified outside the class.
– Caffé