0

Say I have: class A depends on class B depends on class C depends on class A.

It seems impossible to compile. I have read this post about disabling compile-time depedency-checking, but all my classes are within the classpath, well-defined, etc. The only problem is that they mutually depend on each other.

Is there a way to write such an application that would compile without a hitch?

6
  • this is not necessarily and answer, but i have to ask -- how did you start writing that code? did you write class A first? or class B? or class C? chicken/egg? :) Commented Apr 26, 2011 at 16:13
  • When you say "it seems impossible to compile": have you tried it? It should just work. Commented Apr 26, 2011 at 16:13
  • well, javac *.java errors out. Commented Apr 26, 2011 at 16:15
  • OK. Seems I had a problem with a constructor. Now, what answer do I choose...? Commented Apr 26, 2011 at 16:20
  • (for the sake of it) @Liv: all at the same time; no chicken nor egg. @Peter & @Paulo: Thanks for the details. Commented Apr 26, 2011 at 16:24

3 Answers 3

6

We do have several such dependency cycles in our legacy codebase and they do compile without a hitch.

This is not to say it is good to have cyclic dependencies - on the contrary. I intend to get rid of them eventually to clean up our architecture. Nevertheless, in the meantime, the code still compiles and works.

Sign up to request clarification or add additional context in comments.

2 Comments

how do you write them then? Is there a way of referring to classes that I'm doing wrong?
@passcod, class A has a property of type B, B a property of type C and C a property of type A. What kind of dependencies do you have?
1

The important thing here is that the compiler must be able to compile all the classes at the same time. If this is the case, there should be no problem. Of course, you should take care of the usual directory layout problems.

If the packages can't be compiled together, it gets more complicated - you might have to create dummy implementations first (which don't depend on the other classes) and then (when you have the right classes) substitute them. But I can't really imagine an reason for not being able to compile them together.

Comments

0

You can have this circular dependancy like this as Java knows which files to read to find the code from the name. i.e. it compiles them all at once. You will only have a problem if you try to compile one at a time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.