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 ...

Get Apex Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.