from a basic learner! Is it best to create objects and set their initial state by a constructor or use a set of setters? Although if I understand it well setters are mutators is their any reason to not prefer them over constructors?
Thank you
from a basic learner! Is it best to create objects and set their initial state by a constructor or use a set of setters? Although if I understand it well setters are mutators is their any reason to not prefer them over constructors?
Thank you
There is no harm using setters over constructor.
However, there is a difference between setter
and constructor
. If you use a parameterized constructor, you have to pass those attribute values and they kind of become mandatory. With setters, they are optional.
If you already know the attribute values before the object is created, it is preferred to use constructor over setters. There is a time window from creating an object and using it. If the attribute values are not set within that window, there is a change of getting a NullPointerException
if the object is used. So, it is preferred to use constructor to be sure that the object is created with necessary attributes.
Constructors are good if you have a small number of attributes to set, e.g. 2-3 attributes.
If you need to initialize an object that has 10 or more attributes, it can be hard. You have to calculate what position corresponds to what attribute. If let say attribute 7 and attribute 8 out of 12 are both strings, then you can easily pass to parameter 7 value intended for parameter 8, and you will not even notice it, because they have same type and there will be no compile error. This can lead to a bug that requires much time for analysis.
That's why if you have more than 3-4 parameters in constructor, I recommend to use setters instead. When you use setter, you see clearly what value is passed to what parameter. E.g. if you write setName(address), you will notice it quickly and will fix it. Where as in a constructor you could pass address variable to a name parameter and would not notice that.
As a convenient way to use setters you can use builders.
To your comment Setters can't be used to set the inital state of an object?!: It is opinion based. Some consider object as initialized after contructor completed. The others including me call initialization all steps needed to prepare the object to be used in some application logic. For instance, if you create an instance of java.util.Properties, then call its method load(). I say it is initialized only after this method is called. But as I said there are different opinions about that.
Constructors are used for object initializations and setter for future changes to the attributes
A constructor is simply a method that is called when a new object is created, its purpose is to initialize everything which every object of the class must have - the basic stuff.
If you think you would later need to modify any fields, then provide setter methods.
The difference is partly semantic and partly functional. Functionally, if you need to initialize a field to have some state upon instantiation, there is no difference between using the constructor and creating the object and calling a setter. However, the constructor enforces that this field must be initialized, whereas the setter does not. Furthermore, your setter allows that field to be updated at any point in the lifetime of the object thereafter, while your constructor does not.
What is more important here, though, is the semantics. Doing this work in the constructor is preferred as it makes explicit the dependencies of the class. If someone else were to come along and read your code, it would be clear to them what is required in order to make use of your class.