I created a constructor and, according to my professor, I am violating encapsulation, saying that there is a shallow copy in the constructor. I am writing this question hoping that I might see what I am doing wrong. Because I honestly don't know what it is since my fields are private and I also created getters and setters (not shown).
The Code:
public class Class {
private int difficultyLevel;
private String subject;
private List<Student> students;
public Class(final int theLevel, final String theSubject,
final List<Student> theStudents) {
if (theLevel < 0 || theSubject.length() == 0) {
throw new IllegalArgumentException();
}
if (theSubject == null || theStudents == null) {
throw new NullPointerException();
}
difficultyLevel = theLevel;
subject = theSubject;
students = new ArrayList<Student>(theStudents);
}
Thank you very much in advance!
Shallow copy is when you copy the references of objects in a data structure instead of copying the objects themselves and saving a copy of them (deep copy) In this case I guess he is talking about the array of students. He probably wants you to save an array of a copy of each student (new student)