I am creating an application android
with java and I have always instantiated a class in the following way:
Pedido pedido = new Pedido();
pedido.setCantidad(1); // aqui le asigno los valores
But I found that sometimes it is done through a constructor:
Pedido pedido = new Pedido(1);
Which of the two forms is the optimal or the standard? It doesn't give me a specific error, but if I use the second way I can no longer use the first.
First, something particular to java.
All of our classes have a default constructor, with no parameters. That's why you can do:
Now, when you create one or more parameterized constructors, this default constructor is lost, in order to use it you have to explicitly declare it in the class. For your example it would be something like this:
This way you can already use any of the two (you can even have more).
Now, for your specific question, the following occurs to me. The necessary attributes should be in the constructor so that the object can work correctly when it is instantiated. Imagine that I am going to use your Order class. I don't have to know how it works internally, I should only be interested in the public API that you expose in your class (the public methods). It shouldn't know anything about object initialization. Therefore, for the request in particular, either of the two constructors could be used.
There is no "optimal", "standard" or "correct" way. It all depends on the context or the requirements you have.
Perhaps the business you are modeling does not need a quantity (or any other attribute) to be indicated when creating an order. In this case you can make use of a default constructor or without parameters and when you need to define the value of the attribute, you pass it to the instance.
We must bear in mind that we can use this same mode in the event that we have values that we must use by default when instantiating the class, since these values are either defined in the declarations of the class fields or in the body of the constructor. .
If, on the other hand, our business tells us that each time an order is created, a series of values must be assigned to it, which can vary with each order (for example), then perhaps it would be more correct to define a constructor that receives those values.
In the world of software development, we almost always have several alternatives or ways of doing things, but the best one always depends on our specific case. Sometimes what is best for X thing, is not for another.
Both forms are correct. Its use depends on the problem you are solving.
Let's assume that the class
Pedido
was defined as follows:When using this class, it is done as follows:
Here a class is being instantiated but a constructor is still implicitly executed (we don't see it, but the compiler takes care of assigning it to the class).
But what if we add a constructor to it that has overloading?
If we happen to overload a constructor, we could instantiate it in two ways.
So our class would be:
The way to use the class would be:
The code above would compile because the class has two overloaded constructors, otherwise it would compile if it only had one.