O'Reilly logo

Apex Design Patterns by Anshul Verma, Jitendra Zaa

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required