Many times the variables of a class in java are declared as private to have encapsulation.
Other issues to declare a variable as private is so that it can only be accessible from the same class, but finally in most cases you end up doing set and get methods to modify the variable or to get it outside the class.
So what I'm wondering is if there's any good reason to declare a private variable and put a set and a get instead of declaring it public and avoid a set and a get .
Allowing free access to class attributes is not a doomed design, it's just different. This is usually avoided so that the class internally handles any business logic it might attach to the fields. For example:
If we then wanted to add validations to this code, the situation is complicated. Sure, in the example above you can't see it because there are only about 3 calls to
Foo#listaNombres
, but if it were scattered across dozens of methods the situation gets complicated. In addition, it also complicates if the definition of the attribute (name, type, etc) were changed, due to all the changes that would be made. An additional problem is that when getting the list ofFoo#listaNombres
we don't want to pass the list directly, instead we want a copy of it. We could make the copy at each location offoo.listaNombres
but that leads to duplicate code (well, multiplied rather than duplicated).get
The use of the andset
(preferably public) methods is according to the JavaBean specification :Translated:
This means that the get and set methods allow access to the properties (attributes) of a class. Many frameworks benefit from this definition. To name a few:
Placing an example from Spring with XML configuration:
The Java class associated with this bean:
JSF example for field association using Expression Language:
The associated Java class:
Also, the "nice" (?) thing about using these methods is that you can add business logic to them to avoid direct contact with the objects you interact with. From the previous case that we indicated that we want to get a copy of the list instead of getting the list directly, we could add such logic in the get method:
If generating the code for these methods seems too much effort, you can use lombok which offers facilities to add these methods via annotations in your code, among other benefits.
Adapted from Private List with Getter/Setter vs Public List
You've just captured one of the most common questions from developers, "Do I use properties or do I access class attributes directly?" In the other answers you have good examples, I can put a simple one:
Versus
The second segment uses properties to control the value of the attribute, but although it makes use of one of the basic principles of object-oriented programming, it doesn't make sense if we don't require (or will require in the future) to control the inputs/outputs of that attribute. .
You do not need to apply all the theoretical concepts that you know at first instance, many of them you will have to apply as your application matures or scales, control needs increase, but the business logic will tell you whether or not you should encapsulate the attributes of your application. class. Sometimes too much encapsulation ends up creating code monsters like:
When it can be more readable as:
But as I said before, it will depend on the business logic of your application which one to use.
Yes, there are good reasons to use them, although they should not be considered universally necessary. If you declare a variable (or field ) public, you allow direct access to its content without any control measures. In many cases this does not have to be a problem, but with a growing system it can be.
Implementing methods to access the variable gives you a possibility at any time to add code that controls access to the variable without having to change the interface of your class. I give you an example.
Let's consider this class:
compared to the version with
get
andset
At some point we realize that we have to guarantee that at no time name or surname remain
null
.With first class that's basically impossible,
persona.nombre = null;
it's nothing we could ban. However in the other version there is a super easy way:and we get an exception if someone tries to set null.
A more practical example would be that you have to implement a timestamp to back up the update data. Without getters and setters we would have to make sure to check all the code in which the fields could be modified. The practice of using default setters allows us an easier path:
Thus it was possible to implement this functionality only in the model in a class.
On the contrary, a paradigm in which getters and setters are completely redundant are immutable VO s ( value objects, value objects ). An example:
In this case getters or setters would be completely meaningless.
As you can see, yes, there are reasons, but it all depends on the design and context.
The Gets are the outputs and the Sets are the inputs of the class. It is considered a good practice to implement them mainly for security reasons since we maximize encapsulation.
We isolate the actual representation of the data and would only have access to the behavior.