Say I have a class A that has a list of related elements (the type of elements doesn't matter):
public class A {
private List<String> list;
public List<String> getList() {
return list;
}
public void addElement(String element) {
list.add(element);
}
}
Now I want to access this list from another Client class. I need to add a new item. A more philosophical question is how best to do this from a design point of view.
public class Client {
private A a = new A();
public void method1() {
a.getList().add("");
}
public void method2() {
a.addElement("");
}
}
If anyone can point out any advantage of any of these methods, it would be very appreciated. Thank.
source
share