I've been reading about this for a while now and I can't figure it out. Consider these classes, both in their own file.
public class World {
private List<Entity> entities = new ArrayList<>();
public List<Entity> getEntities() {
return entities;
}
public void setEntities(List<Entity> entities) {
this.entities = entities;
}
}
public Class Entity {
public Entity() {
List<Entity> modifiedList = World.getEntities().add(this); // 1
World.setEntities(modifiedList);
}
}
There's a type mismatch: I cannot convert from boolean to List (1) How would I solve this? How could you possibly convert a boolean to a List? Also, the concept static confuses me. If anyone can direct me to some light, and accessible read about static vs non-static, please post it in the comments!
getEntitiesin the variablemodifiedList, then invokeaddonmodifiedList?World.setEntities()is useless. All you need here isWorld.getEntities().add(this);Listin your code above. There is no need to return a "new"List, because there is no newList. The call togetEntities()returns the actualListthat is in yourWorld. It does not make a copy in any way. Adding items to this list adds them to this sameList, the one that is in theWorld. Ergo, there is no need to return a newList, as you are modifying the onlyListin your program.