A circular dependency occurs when two classes depend on each other for creation or operation. If both classes require each other through constructor injection, object creation becomes impossible because each object needs the other to exist first. To resolve this, the dependency should be delayed using setter injection, factory methods, dependency inversion, or redesigning the relationship to reduce coupling.
Suppose ClassA handles order processing and ClassB handles notifications. If both classes directly depend on each other through constructors, object creation fails. Instead, create both objects first and then inject the dependencies using setter methods.
class ClassA {
private ClassB classB;
public void setClassB(
ClassB classB) {
this.classB = classB;
}
}
class ClassB {
private ClassA classA;
public void setClassA(
ClassA classA) {
this.classA = classA;
}
}
public class Main {
public static void main(String[] args) {
ClassA a = new ClassA();
ClassB b = new ClassB();
a.setClassB(b);
b.setClassA(a);
}
}A concise interview answer is: Constructor-based circular dependencies can be resolved by using setter injection, factory methods, or redesigning the classes to reduce coupling. The most common approach is to create both objects independently and inject the dependencies afterward, breaking the circular construction chain.
Last updated: 16 July 2026
Previous question: What are the different types of class loaders in Java?