I'm reviewing object-oriented programming a bit, but there is one thing I don't quite understand, and that is why use the constructor or use getters and setters to give values to the objects in question.
To begin with, I understand that the Getter must be used to be able to see the data, since if we encapsulate it, it does not allow us to access it.
But I don't understand the setter very well, why in theory can't the variables also be passed through the constructor? Why then create the setter? To be able to edit it once it has been created?
If something I said is not understood, I explain it again
public class Persona {
private String nombre;
private int edad;
public Persona(String nombre, int edad) {
this.nombre = nombre;
this.edad=edad;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getNombre() {
String nombre = this.nombre;
return nombre;
}
}
public class CrearPersona {
public static void main(String[] args) {
Persona persona = new Persona("Fernando",12);
System.out.println(persona.getNombre());
persona.setNombre("Luis");
System.out.println(persona.getNombre());
}
}
Exactly, you said it. The idea of setters is to be able to modify the internal state of your object, once it has already been built. Many times we don't want that state to be modified, so we can not add setters, and make all instance variables
final
, in principle that would make our objects immutable, once built, it would not be possible to change the state. But you have to be careful, because some properties may not be immutable in themselves, for example a variable of typejava.util.Date
, evenfinal
if it is accessible from outside the class because it precisely has a getter, you could change, not the object, but the value of it.For example:
Here, although the date is final, its value could still be modified.
For the same reason: the
encapsulacion
.All properties of your object are supposed to be private, and can only be accessed with a public(
setter
) method.In addition, in this
setter
, you can put your own restrictions, such as which ID number is figures and numbers or whatever comes to your mind.With the constructor you are right, you can pass through parameters, but imagine that it is an unknown parameter.
You have no choice but to create the constructor without that parameter (or with overloading) and create a
setter
.Getters and setters are used to access attributes in a controlled way, so no class can modify the values of said attributes from outside of them, it is an encapsulation principle, at the same time, you can code certain conditions or make changes in the input data before passing it to the class attribute.
The constructor is used to define what happens when we create an instance of a class using the new operator, the constructor allows us to assign default values to the fields or call another constructor or method if desired.
Hello! We know that encapsulation in Java is used to hide methods and attributes (variables) of a class. This is useful when sharing our classes with other programmers.
However, sometimes it is necessary or required to grant access to some attributes of our class without removing the encapsulation, since having shared access is not to be considered publicly.
For these situations there are setter() and getter() methods.
These methods are used to give access to the attributes of a class that are privately encapsulated in order to obtain data from the encapsulated attributes.
The setter method is used to assign a value to an attribute of our privately encapsulated class. This is done directly with this method. Since this method does not return anything, it must contain the word void in its structure, and must always receive an input parameter.
And the getter method accesses the class to return the value of any attribute that we want. This method must return a value, so the structure of this method must contain the type of value that we are going to return with that method.
Defining an attribute in the constructor is used to guarantee that if the object is created, it will necessarily have that attribute defined.
Notice what happens if you call getParameter after creating the objects:
As you can see, if between the creation and the setter you access that attribute, it will be undefined.
This does not mean that you always have to define the attributes of the constructor, there are cases that due to the nature of the program you are doing, there are some attributes that have to be undefined when creating the object and you have to give them that value later. The setter also comes in handy if the attribute value needs to be changed multiple times, since the constructor is only executed once for a given object, but the setter can be called as many times as needed.