(Java) Can I use a constructor to write into an array?

Title. If I read from a .txt file, can I read it into a constructor than into an array? Or should I just use mutators like I currently am.

A mutator is usually written to modify a local (private) field. Your constructor can create and update the local field without using the mutator.

E.g.

public class ThingHolder {
  private Thing[] mythings;

  public void setThing(Thing newThing, int thingdex) {
    mythings[thingdex] = newThing;
  }

  public ThingHolder() {
    List<Thing> readThings = ReadThingsFromFile();
   mythings = readThings.toArray(new Thing[0]);
  }
}
2 Likes