I met such a construction in the training example.

public class Vehicle { private String color; //Constructor Vehicle(String c) { this.setColor(c); } // Setter public void setColor(String c) { this.color = c; } } 

As far as I understand, the setter is used to set the value of a private variable, which the designer is also capable of. Why use a constructor that uses a setter that assigns a value to a variable if you can get away with the constructor? There are cases when it is necessary, or is it just a theoretical example that this is possible?

    2 answers 2

    A setter does not always just assign a value to a variable, most often it also performs some actions (normalizes data, for example), so in order not to duplicate the code from the setter in the constructor, you can simply call the setter from the constructor.

    • 1) That is, the value of a variable assigned in the constructor when creating an object cannot be changed in any way? I did not know ... 2) About the setters, I still know the most general theory, I don’t know what it means to "normalize data". But thanks a lot, I'll keep in mind. - Rumata
    • 2
      @MaximVelichkin meant that a private variable cannot be changed directly from the outside. Those. Vehicle vehicle = new Vehicle("red"); , vehicle.color = "green"; it is no longer possible to do without a setter ....... or a method of this class that will manipulate this variable inside - Alex Shimansky
    • 2
      1) The value of a private variable assigned in the constructor in the absence of a setter cannot be changed. If you have a setter, you can change them through it. 2) Well, suppose you need to assign the Integer value to a variable, but you need it to always be more than 0 and less than 256. For this, you can perform the appropriate checks and conversions in the setter, while in other places of the code without thinking about it to call the setter . - Aim X
    • 1) I understood, I thought that it was impossible to change at all 2) Now I understand, thanks a lot! - Rumata
    • one
      Well, I will correct the answer. - Aim X

    Bad learning example. I would not recommend using setters inside a constructor, especially if the setter is simple. If the setter contains a lot of things, then in this case it is better to revise the structure of the class, perhaps it would be better to use the Bilder pattern to create an instance of the class. The main problem with the setters inside the constructor (or rather, with the setters that can be redefined) is that they can create a lot of problems if the method is redefined.

    Quote from the book JAVA: Effective programming. 2nd edition, Joshua Bloch. Section 17.

    Class constructors should not call redefined methods , directly or indirectly. Violation of this rule may cause the program to crash. The superclass constructor is executed before the subclass constructor, and therefore the overriding method in the subclass will be called before the constructor of this subclass is launched. And if the overridden method depends on the initialization that the subclass constructor performs, then this method will not work as expected.

    • Thank you very much, I will keep in mind, although I don’t understand everything yet. - Rumata