0

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!

Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
  • 1
    Why don't you just store the result of `getEntities` in the variable `modifiedList`, then invoke `add` on `modifiedList`? – Sotirios Delimanolis Nov 16 '15 at 17:36
  • @SotiriosDelimanolis Isn't that just moving the problem from line 1 to line 2? – Bram Vanroy Nov 16 '15 at 17:38
  • See the javadoc for `boolean Collection#add(T)` – fge Nov 16 '15 at 17:38
  • Using `World.` is attempting to call a static on World. Your World methods are not static, and instead pertain to each world object. You need to pass in a World object in the Entity constructor `public Entity(World w)` and use `w.getEntities()` instead of `World.getEntities()` – phflack Nov 16 '15 at 17:38
  • 2
    Also, you need to realize that calling `World.setEntities()` is useless. All you need here is `World.getEntities().add(this);` – JB Nizet Nov 16 '15 at 17:38
  • @BramVanroy No. You wouldn't reassign the result of `add` to anything. Or what JB just said. – Sotirios Delimanolis Nov 16 '15 at 17:39
  • @SotiriosDelimanolis So if I get it right, by using .add you can use a getter method as a setter method? Because the initial value of entities is private, and you only *get* its value with a getter, but then you can modify its contents by using add? But how then is the new list returned? – Bram Vanroy Nov 16 '15 at 17:40
  • `getEntities` returns a value. That value is a reference to a `List` object. You can use it to invoke methods of that `List`. – Sotirios Delimanolis Nov 16 '15 at 17:41
  • If you think so, please clean up your question to be specific about which issue you're asking about. – Sotirios Delimanolis Nov 16 '15 at 17:42
  • 1
    Forgetting, for the moment, the static issue ... as it is implemented, there is only one `List` in your code above. There is no need to return a "new" `List`, because there is no new `List`. The call to `getEntities()` returns the actual `List` that is in your `World`. It does not make a copy in any way. Adding items to this list adds them to this same `List`, the one that is in the `World`. Ergo, there is no need to return a new `List`, as you are modifying the only `List` in your program. – dcsohl Nov 16 '15 at 18:16
  • @dcsohl I was confused with the power of lists. I thought you'd only be able to change the contents of a list throught a getter method, embedded in a setter method, because you'd nee the setter to set the list to the old list + new item. Apparently that's not necessary in lists! – Bram Vanroy Nov 16 '15 at 18:30
  • 1
    That's not a feature of Lists, it's how java's references to objects (such as Lists) work: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – zapl Nov 16 '15 at 18:53

1 Answers1

1

You've declared a class

public class World {
    private List<Entity> entities = new ArrayList<>();

    public List<Entity> getEntities() {
        return entities;
    }

    public void setEntities(List<Entity> entities) {
        this.entities = entities;
    }
}

which declares two instance methods, getEntities and setEntities. These are instance methods, so you need an instance to invoke them. That's what that duplicate was saying.

World world = new World();

World#getEntities has a return type of List. List has an add method with a return type of boolean, where the value of the boolean typically indicates whether the value was added or not. If you don't need that information, ignore the returned value

List<Entity> entities = world.getEntities();
entities.add(this);

getEntities returns a reference to the same object referenced by the field entities in the object referenced by world in my example above. There's no point invoking setEntities to re-assign it.

Further reading:

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724