title: How can I prevent infinite recursion when declaring two object each containing the other?
body: <p>its my first question and I am already nervous. I may start with "Its for my homework" and it is but i try to get to the problem:
  I have to realise a simple ticket selling program, that contains two classes:
  a class ticket which contains the following fields:</p>
  
  <pre><code>public class Ticket
  {
      //Fields
      private int ticketID;
      private boolean sold;
      private Category category;
  
      //Constructor
      public Ticket(int ticketID, Category category)
      {
          this.ticketID = ticketID;
          this.category = category;
          this.sold = false;
      }
  }
  </code></pre>
  
  <p>and a class named Category which should look like this: </p>
  
  <pre><code>public class Category
  {
      char category;
      double sellPrice;
      Ticket[] tickets;
  
      private Random rnd;
  
      public Category(char category, double sellPrice, int amount)
      {
          this.category = category;
          this.sellPrice = sellPrice;
  
          //from here on my difficulties start!
          rnd = new Random(System.nanoTime());
  
          tickets = new Ticket[amount];
          for(int i = 0; i &lt; tickets.length; i++)
          {
              tickets[i] = new Ticket(generateRandom(100000, 1000000), this); 
              //especially this line worries me!
          }
      }
  </code></pre>
  
  <p>For simplicity I stripped the code down to bare mimimum. Now my problem is that I should create an array of Category which will contain the arrays of Tickets, but when I do that I create an infinite loop by referencing each ticket to the Category object by passing it the "this" for the category parameter of its constructor. Whel debugging my project I really create an "infinite" loop because I can always go deeper into the combination of Category->tickets[]->category->tickets[]->... and so on. The Assignment says I have to use the Category class in Ticket but is there another way to create a system without these "loops"?</p>