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?

Community
  • 1
  • 1
Félix Saparelli
  • 8,424
  • 6
  • 52
  • 67

3 Answers3

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.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
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.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130