-2

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
  • 3
    via builder or setter Commented Nov 11, 2020 at 9:01

1 Answer 1

0

Another way can be via a setter method

// 2. Setter method
public void setFieldList(List<String> fieldList) {
    this.fieldList = fieldList;
}
Sign up to request clarification or add additional context in comments.

2 Comments

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?
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!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.