Instantiating an entity within another entity: how to do it?

Asked

Viewed 207 times

1

The class Estado depends on the class Pais. What would be the difference between me doing this:

Código

And do this?

Código

I mean, what’s the difference between me instantiating the class Pais within the class Estado to instantiate the class Pais in a main class and then I assign the Pais at the Estado?

  • It would be interesting to put the formatted code in question instead of images.

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site

1 answer

2

Contrary to what the other answer says, it is not a question of performance, unless you are talking about the quality of this particular code. The issue is construction warranty. Read What good is a builder?.

Now you may be thinking: but this is not a builder, as this question here may be about this?

It turns out that what you’re doing is actually put on a builder. Even if you didn’t create one it will be created by the compiler to put the code that initializes the object. Up because the data structure does not perform anything, everything runs in methods and the constructor is a method. You are not seeing but the actual initialization is in a constructor method. The only magic is this change of place of the code made by the compiler. JVM does no magic, does not boot faster or differently.

The only advantage is that whenever the object is created it is guaranteed that this value will already be there in the object before the JVM gives control to its consumer code. Just remembering that in this case pais will receive a pointer only after running the constructor Pais() and get the address of that other object that will be in another area of memory - possibly right away. It makes no difference whether this allocation is made within the class (in the constructor or elsewhere) or in another method outside the class, unless the allocation location can be slightly different because the allocation order can be different if initialize in another class.

I do not remember in Java whether the construction in this form guarantees some order, I believe, so do not take as absolute truth, that the initializations occur in the order that were declared in the class, but without guarantees. Surely these initializations would occur before others made explicitly within the constructor created by you (it will be the same constructor, it is only a matter of the order that the compiler puts).

Without putting in the class you are letting free that this object is created without a value in that field.

The use of getter and mainly Setter is often used mistakenly in almost every code, especially by beginners who learn the cake recipe and not their motivation (and almost always should not exist). Has links where this is shown and even discussed. For example, a Estado may change Pais? When he’s created, he could go a while without one Pais()? Why?

On the other hand this code has another problem in the creation of Pais(). Which country is being created? A null country without information? Strange, is it in another situation to enter a valid state? It is more severe within the class, because if an invalid object is created there it can possibly be invalid forever, at least if the code is done with the constructor and the state modifier pattern correctly, which this code did not. Because that data must be mutable?

In the current form of the code there is no point in creating that object in the class. It is initiating the field pais with invalid object (for what?) and which should be initialized later. And then on main() is invalid initiating for a while, luckily the programmer, I believe, built the apparently correct object before storing it in the estado. If the code leaves and even induces to do wrong things it is all wrong, even if it works.

Fiat 147 todo detonado andando pelas ruas

If you consider that you are allocating two completely different objects (one in the class and the other in main(), one of them making the other unnecessary and abandoned right away, then you can, in this particular code that’s wrong, have a little extra cost of performance because creating an extra object has a cost and puts pressure on the Garbage Collector that will have to deal with this object created at the initialization of the class that was never actually used.

The logic of creating ids then asks to give problem, should not leave in the hand of the programmer to take care of it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.