-3

What other ways are there to initialize the class field "fieldList" based on a given List object? One way would be Parameterized constructor.

class ObjectA {
  private List<String> fieldList;

  // 1. Parameterized constructor
  public ObjectA(List<String> _fieldList) {
    this.fieldList = _fieldList;
  }
}
Mihai Socaciu
  • 155
  • 2
  • 12

1 Answers1

0

Another way can be via a setter method

// 2. Setter method
public void setFieldList(List<String> fieldList) {
    this.fieldList = fieldList;
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 1
    Ah, I was thinking the question too hard, with initialization blocks. I got it at an interview and I thought there is some hidden trick. Should I remove the question since it became negative? – Mihai Socaciu Nov 11 '20 at 09:22
  • You are most welcome. You should keep the question as it may be helpful for other beginners. Everything is difficult before it becomes easy. There is no need to get demotivated by the negative. Even experts get that. As a learning experience, you need to do some more research and put some more facts when you post your next question. I wish you success! – Arvind Kumar Avinash Nov 11 '20 at 09:56