Is it possible (or sensible) to use an object in it's own constructor?(Sorry for the poorly formulated noob question)
Say I have a class "Students" which contains an arrayList of subclass Student and a method for adding new students to the array.
Can I in my Student constructor use the addStudent method to add the new instance to the array on creation?... like so:
//Students
class Students{
private static ArrayList<Student> STUDENTS = new ArrayList<>();
public static void addStudents(Student student){
STUDENTS.add(student);
}
}
//Student
class Student /*extends Students <- old misstake left for reference*/{
private String name = "";
private int birthYear = 0;
Student(String _name, int _birthYear){
this.name = _name;
this.birthYear = _birthYear;
//insert wild guess
Students.addStudents(this(name,birthYear));
}
}
Or will this simply loop and create a lot of objects until everything crashes?
STUDENTSstatic? Also, don't make it all capitalized unless it is a static final variable.Student extends Studentssounds weird .addStudents(this(name,birthYear));toaddStudents(this);if you want to add the new Student to the static List.Studentshould not extendStudents. A student is not a kind of students.