Circular dependencies
There are multiple situations where the developer accidentally creates a circular dependency between Apex classes, triggers, or even in object relationships. There are a few programming languages that detect circular dependency between classes during compilation. Unfortunately, Apex can only generate errors at runtime.
Circular dependency in Apex classes
We will understand circular dependency using the following two Apex classes:
public class ClassA { public ClassA(){ ClassB b = new ClassB(); System.debug('*** Class A Constructor '); } } public class ClassB { public ClassB() { ClassA a = new ClassA(); System.debug('*** Class B Constructor '); } }
As we can see in the preceding code, a constructor of Class A calls a constructor of Class B, resulting in a error if we try to instantiate any class.
Run...