0

Is this better?

public MyClass(String item){
    this.item = item;
}

Or this?

public MyClass(String item){
    setItem(String item);
}

Or are they the same?

I have been taught both ways, but one professor said that the second method was more secure. I'm not sure why though.

Juan David
  • 49
  • 5
  • 1
    the 2nd method is less secure as `setItem` can be overridden – Reimeus Mar 22 '23 at 14:17
  • 1
    Calling methods that a superclass could override in a constructor [is a bad idea](https://stackoverflow.com/questions/3404301/whats-wrong-with-overridable-method-calls-in-constructors). That means **if** you want to call your setters, they should be `final`. – Joachim Sauer Mar 22 '23 at 14:17

0 Answers0