-1

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

Rami
  • 67
  • 6
  • Depends on the use-case. If you want a mutable object, use mutators. If you want an immutable object, then don't. – Turing85 Jan 17 '20 at 20:03

5 Answers5

1

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.

Rishikesh Dhokare
  • 3,559
  • 23
  • 34
1

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.

mentallurg
  • 4,967
  • 5
  • 28
  • 36
0

Constructors are used for object initializations and setter for future changes to the attributes

mdev
  • 349
  • 1
  • 5
  • Setters can't be used to set the inital state of an object?! – Rami Jan 17 '20 at 20:05
  • 1
    They can be called from the constructor. But the cannot be called on a reference before the object was constructed (through a constructor). If one tries, one will get a `NullPointerException`. – Turing85 Jan 17 '20 at 20:07
  • @mdev: This is not always true. There are many cases when putting initialization to setters is better than putting it to constructor. – mentallurg Jan 17 '20 at 21:10
0

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.

  • How about having 10-15 parameters in constructor? How about impossibility to extend/override logic that is implemented in constructor? There are many cases when using setters is a preferred way to initialize an object. – mentallurg Jan 17 '20 at 21:09
  • That's not a reason to down vote my comment. the OP is a beginner and wanted to understand the difference between the 2 ways - so I explained. Although his question is general and can't be answered within 1 comment, it's still enough to give the OP enough understanding. – Techno Freak Jan 18 '20 at 02:29
0

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.

Azar
  • 1,086
  • 12
  • 27
  • *it would be clear to them what is required* - It would be clear only if the number of parameters in constructor is relatively small, say 2-3. If you have 10-15 parameters (e.g. you want to initialize a bean representing a row in database table), it can be pretty hard to understand what positions in constructor means what. That's why in some cases constructors are preferable, in other cases setters. – mentallurg Jan 17 '20 at 21:02