0

Is it okay to use an empty constructor in Java? For example, when loading the data from a MySQL database, I want to do something like:

ResultSet set = statement.executeQuery();
while (set.next()) {
    Faction faction = new Faction();
    faction.setId(UUID.fromString(set.getString("id")));
    faction.setName(set.getString("name"));
}

Because I already have a constructor for the faction class,

public Faction(Player player, UUID uuid) {}

I was wondering if I can have a plain constructor and just set the values as and when.

Otherwise I could create a constructor using parameters that match the mySQL data (public Faction(String name, UUID uuid, String announcement..etc), to load in.. Not sure what is best practice?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
J. Doe
  • 105
  • 1
  • 6
  • 2
    Yes, it's absolutely fine to have both parameterless and parameterized constructors. See `StringBuilder` for examples... – Jon Skeet May 10 '17 at 19:34
  • "Otherwise I could create a constructor using parameters that match the mySQL data" -- Might as well use an ORM approach at that point – OneCricketeer May 10 '17 at 19:36
  • ^ + Empty constructor is generated automatically, you don't even have to write it. Although, sometimes you'd have to define an empty, parameterless constructor yourself in order for some framework to be able to work with it (it'll throw an error if there's NO empty constructor). – Cargeh May 10 '17 at 19:36
  • Please read [Why is asking a question on “best practice” a bad thing?](https://meta.stackexchange.com/questions/142353/why-is-asking-a-question-on-best-practice-a-bad-thing/243450) before attempting to ask more questions that are opinion based that invite argumentative discussion because they do not have a single agreed upon answer. –  May 10 '17 at 19:46

2 Answers2

5

If the object state should not change when the Faction class is instantiated, provide a constructor with args and removing setter is better.
In this way, you avoid undesirable behavior.

Now, according to your saying, you probably need to set many String parameters.
Doing it with a constructor is very error prone as you may do a mistake in the parameter order when you use it.

To achieve your need, you have two main ways :

  • using an empty constructor and then setters as you propose (desirable if the object is mutable)

  • if your object is immutable, you could use the Builder pattern to construct an immutable object (You write something like : Faction faction = new Faction.Builder().name(name).uuid(uuid).announcement(announcement).build();

davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

It depends on your use case. If you dont want the class variables to change once you set them then in that case declare those variables as final and use the parametised constructor. If you want the variables to change once you set them, then use the default constructor with setters and getters.Both the options are perfectly fine.

Ezio
  • 2,837
  • 2
  • 29
  • 45