Should we create an empty constructor in Java?

Asked

Viewed 292 times

6

It is good practice to always declare a builder, even if it is empty, for the class?

I find it unnecessary, because the compiler automatically creates. I have seen people who always create and vehemently defend that it is bad practice not to declare. What is right?

  • do not necessarily see this as a good practice, but in some cases, if the class is accessed via Reflection it is quite possible that you have an unexpected behavior

  • @Lucasmiranda can give more details?

  • i was thinking about the possibility of some framework accessing the class through newInstance(), but thinking about it doesn’t make sense

  • 1

    @Even if a constructor is not explicitly declared, the default constructor will always be there, and in cases of Reflection it would still be possible to access it using Class.getConstructor() and then call the Constructor.newInstance()

1 answer

5


It doesn’t make sense. You know exactly why it doesn’t make sense. What you get by doing what the compiler already does for you exactly the same?

In fact I go further, in most cases it makes sense to create other constructors, which disarms this ability of the compiler to create a standard builder (the name of that mechanism), which in that case if you needed one you would have to create manually. But in practice almost every time you do this there must be something wrong, almost always do not need and should not have the default constructor, nor generated by the compiler.

The default constructor exists because it cannot be without one, but it is usually a mistake in robust applications. Of course there are situations where there really is no reason to create something more complex, a typical case is an anemic object.

Obviously if makes sense to have a default constructor (no parameters) and it must have some logic inside which is not empty or only to initialize the members of the object (this is what the constructor the compiler generates does), so it makes sense to create this constructor.

Have a question about the importance of the builder. Most created classes that do not have a constructor with parameters must be wrong, even if they work. You are only right if you always want to initialize with the default values of each type of object members.

When someone defends something, ask them to explain why, and then they can confront other people in more detail. If you don’t have why, ignore it, it may be right, but without knowing why you have no reason to do it.

  • 1

    I see that usually people who talk about always creating a builder are the same people who create getters and setters for no real reason and without even thinking about whether it will be necessary or useful, will just in the automatic IDE create everything without questioning the real need for each thing

  • 1

    @nullptr exactly! People memorize rules, but they don’t learn why. The less API the better, just create what will be useful. And so almost always the default constructor even created by the compiler is not good in many cases. And I’m not even going to go into the question of API seeming to be useful and not being because it enters a bigger subjectivity.

Browser other questions tagged

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