Answer the question
In order to leave comments, you need to log in
What are getters and setters for?
Is it important to use them in method names?
Answer the question
In order to leave comments, you need to log in
This is one of the conventions for a class to be used as a Java Bean .
Class properties must be accessible via get, set, and other methods (called accessors), which must follow the standard naming convention. This easily allows tools to automatically detect and update bean content..
The first principle of OOP is encapsulation. Access control to fields and methods of a class. How you implement them is up to you, but you need to make sure that the purpose of the method is clear
It seems to me that the ability to change parameters directly is a potential bug in the code (someone might misuse them), and the get and set methods look more concise, for example, because you give the other programmer information that you can to do with the given class, not a dry variable.
Also, the get and set methods can be modified in such a way that they check or change the incoming and outgoing data as needed, as a primitive example - the getName method, which takes the full name string and extracts the name, without this method, the user programmer should make add-ons on the class to extract the name .
In theory, these names are not required. The main thing is to stick to encapsulation.
Read the section on this topic in head first java. Everything is described in detail there.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question