Is it possible to use indirect recursive class?

Discussions

EJB design: Is it possible to use indirect recursive class?

  1. Is it possible to use indirect recursive class? (3 messages)

    Hi every one.A question occurs in my mind and I don't know if it's reasonable.

    eg:

    public class A{
      B b;
      public A(){
        b=new B();
      }
    }

    public class B{
      A a;
      public B(){
      }

      public void method(){
       a=new A();
      }
    }

     
  2. Don't see any problem with this construct. Instances of clases have references to each other, but there does not seem to be any really recursive calls here, as they are just references.
  3. One reason why I post last question is:the complier must fisrt find the class file(*.class) which another class referenced.So which class file would be first formed?


    how about like this:

    public class A{
      B b;
      public A(){
        b=new B();
      }
    }

    public class B{
      A a;
      public B(){
        if(a==null)a=new A();
      }
    }

      
  4. Of course, now you just a get a stack overflow, because each of your constructors calls another constructor. So there is still no problem here, just don't do that (not that I know why you'd need to).