Using an object within its own constructor

Viewed 270

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?

2 Answers
Related