D
D
deleted-ITema3252015-08-05 21:19:39
Java
deleted-ITema325, 2015-08-05 21:19:39

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

6 answer(s)
E
Evhen, 2015-08-05
@EugeneP2

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.
.
+ A bunch of different xml / json serializers, JPA work on this agreement
+ Simple prefixes to the get and set methods immediately tell what these methods are for.
To be honest, manually writing getters and setters has long been tired and redundant for everyone, therefore in modern JVM languages, such as Groovy or Scala, access methods are generated automatically by the compiler. For Java, there is the Lombok framework , which allows you to automatically generate field access methods.

D
Dmitry Kovalsky, 2015-08-05
@dmitryKovalskiy

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

C
ColdSpirit, 2015-08-05
@ColdSpirit

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 .

S
Sanan Yuzb, 2015-08-05
@Sanan07

In theory, these names are not required. The main thing is to stick to encapsulation.

V
Vladimir Mikhalik, 2015-08-06
@vladiq88

Read the section on this topic in head first java. Everything is described in detail there.

D
Dmitry, 2015-08-06
@Dit81

It's better to call methods and classes "telling" names... getSpeedCar and setSpeedCar immediately speak of their direct application...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question