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!

12
  • 1
    Why don't you just store the result of getEntities in the variable modifiedList, then invoke add on modifiedList? Commented Nov 16, 2015 at 17:36
  • @SotiriosDelimanolis Isn't that just moving the problem from line 1 to line 2? Commented Nov 16, 2015 at 17:38
  • 2
    Also, you need to realize that calling World.setEntities() is useless. All you need here is World.getEntities().add(this); Commented Nov 16, 2015 at 17:38
  • 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. Commented Nov 16, 2015 at 18:16
  • 1
    That's not a feature of Lists, it's how java's references to objects (such as Lists) work: stackoverflow.com/questions/40480/… Commented Nov 16, 2015 at 18:53

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.