No, a public setter is not always needed for a private variable. The idea of providing public setters (and getters, for that matter) is based on what external objects, such as classes, need access to the internals of the specific code that you write. Getters and seters provide this open interface for this. However, you do not have to provide a public getter or setter for each private variable that you create, as this private variable can exist only for internal, private use in the class.
, .
( - getter, ). , . , , ( , , , , ..), . , getSubscriptionFields() , :
public String getSubscribedField(int i) {
return subscribedFields.Get(i);
}
public String addSubscribedField(String field) {
subscribedFields.Add(field);
}
, .