title: How to implement a Many-to-Many POJO structure?
body: <p>I have many Students each one may have many Subjects and each Subject may have many Students as well. Now I want to implement a POJO structure:<code>allStudentList</code> and <code>allSubjectList</code> are just for query all elements of each type.
  <br>
    Is this structure a properly designed?
  
  <br>
    Are there better alternatives?
  
  <br>
    Here is my approach:
  </p>
  
  <pre><code>//THE Student class 
  public class Student {
  
      //List with all students.
      private static ArrayList&lt;Student&gt; allStudentList = new ArrayList&lt;&gt;();
      //Student fields
      private String id;
      private String name;
      //The subjects of Student
      private ArrayList&lt;Subject&gt; subjectsList = new ArrayList&lt;&gt;();
  
      public Student(String id, String subject) {
          this.id = id;
          this.name = subject;
          allStudentList.add(this);
      }
  
      public boolean addSubject(Subject subject) {
          subject.getStudentList().add(this);
          return subjectsList.add(subject);
      }
  
      public String getId() {
          return id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
  
      ArrayList&lt;Subject&gt; getSubjectsList() {
          return subjectsList;
      }
  
      public void setSubjectsList(ArrayList&lt;Subject&gt; subjectsList) {
          this.subjectsList = subjectsList;
      }
  
      public static Student[] getStudents() {
          return allStudentList.toArray(new Student[allStudentList.size()]);
      }
  
      @Override
      public String toString() {
          String s = "";
          for (Subject subject : subjectsList) {
              s += "\n  " + subject.getId() +" " +subject.getName();
          }
          return "Student{" + "id=" + id + ", name=" + name + ", subjectsList=" + s + '}';
      }
  //THE Subject class 
  public class Subject {
  
      //List with all students.
      private static ArrayList&lt;Subject&gt; allSubjectList = new ArrayList&lt;&gt;();
      private String id;
      private String name;
      private ArrayList&lt;Student&gt; studentList = new ArrayList&lt;&gt;();
  
  
      public Subject(String id, String name) {
          this.id = id;
          this.name = name;
          allSubjectList.add(this);
  
      }
  
      public String getId() {
          return id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      ArrayList&lt;Student&gt; getStudentList() {
          return studentList;
      }
      public boolean addStudent(Student student) {
          student.getSubjectsList().add(this);
          return studentList.add(student);
      }
  
      public static Subject[] getStudents() {
          return  allSubjectList.toArray(new Subject [allSubjectList.size()]);
      }
  
      @Override
      public String toString() {
          String s ="";
          for (Student student : studentList) {
             s +="\n  "+student.getId() +" "+student.getName();
          }
          return "Subject{" + "id=" + id + ", nombre=" + name + ", studentList=" +s  + '}';
      }
  //The test class:
  public class TestMany2Many {
  
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) {
          Student student0 = new Student("ST000", "Morty");
          Student student1 = new Student("ST001", "Rick");
          Subject subject0 = new Subject("SU000", "Physics");
          Subject subject1 = new Subject("SU001", "Math");
          student0.addSubject(subject0);
          student0.addSubject(subject1);
          student1.addSubject(subject0);
          Student[] st = Student.getStudents();
          Subject[] su = Subject.getStudents();
          for (Subject subject : su) {
              System.out.println("subject = " + subject);
          }
          for (Student student : st) {
              System.out.println("student = " + student);
          }
  
  
      }
  
  }
  </code></pre>