Different ways to initialize object field with parameter

519 Views Asked by At

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;
  }
}
1

There are 1 best solutions below

2
On BEST ANSWER

Another way can be via a setter method

// 2. Setter method
public void setFieldList(List<String> fieldList) {
    this.fieldList = fieldList;
}