title: Java: classes with instance of each other
body: <p>I feel like this is a really basic question, but I've never got a situation like this. Right now I have a structure like this:</p>
  
  <pre><code>public class Main{
      ClassA a = new ClassA();
      ClassB b = new ClassB(a);
  }
  </code></pre>
  
  <p>My <code>ClassA</code> is needed in <code>ClassB</code> as well as in the <code>Main.class</code>. Now I noticed that <code>ClassA</code> has to know <code>ClassB</code> as well.</p>
  
  <pre><code>public class Main{
      ClassA a = new ClassA(b);
      ClassB b = new ClassB(a);
  }
  </code></pre>
  
  <p>Obviously won't work. First I thought of using a <code>Observer Pattern</code> but this seems to be an overkill to me, besides it's not what the pattern is usually used for. <br>
  Is there a way, that I can make <code>Main.class</code> know <code>ClassA.class</code> &amp; <code>ClassB.class</code>, as well as make <code>ClassA.class</code> know <code>ClassB.class</code> and vice-versa? Since <code>Main.class</code> uses <code>ClassA</code> as well I have to have the same class only initialized once. </p>