1

I'm trying to create Patient and Allergy class and I'm confused about List allergies in setAllergies, whether it should be:

    this.allergies = allergies;

or

    allergies.add(allergies);

Patient class:

public class Patient {

    private List<Allergy> allergies;

    public List<Allergy> getAllergies() {
        return allergies;
    }

    public void setAllergies(List<Allergy> allergies) {
        this.allergies = allergies;
    }

}

Allergy class:

public class Allergy {
    private String name;
    private Severity severity;

    public Allergy(String name, Severity severity) {
        this.name = name;
        this.severity = severity;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Severity getSeverity() {
        return severity;
    }

    public void setSeverity(Severity severity) {
        this.severity = severity;
    }
}
2
  • allergies.add(allergies) makes no sense. It wouldn't even compile. Commented Jul 2, 2017 at 16:59
  • @shmosel The main confusion I do have is that Allergy class will return Allergy object then how is it getting added in the List<Allegry> by this.allergies = allergies ? From where the list of allergies will come from? As allergies is being passed as a parameter. Commented Jul 2, 2017 at 17:10

1 Answer 1

1
allergies.add(allergies);

adds a object to itself.
Why doing that ? Besides you cannot add a List to a List with add().
You have to use addAll() for that.


Actually the allergies field is null when a Patient instance is created.

As a consequence, doing it : allergies.invokeSomething(...); will throw a NullPointerException. Besides, the "set" prefix has a semantics to overwrite/replace, not to add.
So you should keep your method as you wrote for the moment :

public void setAllergies(List<Allergy> allergies) {
    this.allergies = allergies;
}

Now, if you want provide a way to add Allergies to the Allergies objects contained in the allergies field, you should provide a method addAllergies() :

public void addAllergies(List<Allergy> allergies) {
    this.allergies.addAll(allergies);
}

And you should also initialize the allergies field in its declaration (or in the constructor of Patient) :

private List<Allergy> allergies = new ArrayList<>();
Sign up to request clarification or add additional context in comments.

3 Comments

The main confusion I do have is that Allergy class will return Allergy object then how is it getting added in the List<Allegry> by this.allergies = allergies ? From where the list of allergies will come from? As allergies is being passed as a parameter.
"From where the list of allergies will come from? " You have provide as arguments the allergies List that you want to set in the Patient object.For example : Patient p = new Patient(); List<Allergy> allergies = Arrays.asList(new Allergy(...), new Allergy(...)); p.setAllergies(allergies)
Got it. Thanks a lot.

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.