Java – Why is the execution process different for the following cases?

Why is the execution process different for the following cases?… here is a solution to the problem.

Why is the execution process different for the following cases?

Can someone elaborate on the following scenario, it would be more convenient if the explanation included memory allocations and references in three cases:

  1. How is the process performed in the three cases?
  2. Why is the process different in the three cases?
  3. While there is a circular dependency between these two classes, why is 1 executed separately when the rest fails?

Case one

namespace CircularDependency_1
{
    class Program
    {
        static void Main(string[] args)
        {
            A a = new A();
            Console.WriteLine("executed");
            Console.ReadLine();
        }
    }

public class B
    {
        public static A a = new A();

public B()
        {

Console.WriteLine("Creating B");
        }
    }

public class A
    {
        public static B  b = new B();

public A()
        {          
            Console.WriteLine("Creating A");
        }
    }
}

Output

Creating A
Creating B
Creating A
executed

Case 2

namespace CircularDependency_1
{
    class Program
    {
        static void Main(string[] args)
        {
            A a = new A();
            Console.WriteLine("executed");
            Console.ReadLine();
        }
    }

public class B
    {
        public static A a;

public B()
        {
            a = new A();
            Console.WriteLine("Creating B");
        }
    }

public class A
    {
        public static B b;

public A()
        {      
             b = new B();
            Console.WriteLine("Creating A");
        }
    }
}

Output
The process was terminated because of StackOverflowException.

Case Three

namespace CircularDependency_1
{
    class Program
    {
        static void Main(string[] args)
        {
            A a = new A();
            Console.WriteLine("executed");
            Console.ReadLine();
        }
    }

public class B
    {
        public A a;

public B()
        {
            a = new A();
            Console.WriteLine("Creating B");
        }
    }

public class A
    {
        public B b;

public A()
        {      
             b = new B();
            Console.WriteLine("Creating A");
        }
    }
}

Output
The process was terminated because of StackOverflowException.

Solution

@Versatile, you’re close, but not right. The reason why the first case performs and the other two fails is not just because the object was created inside the constructor or inside the class (outside the constructor). To demonstrate this, try making the a and b fields in classes B and A not static (case one), respectively; You will see that it fails, even if the object was created outside of the constructor.

Case

2 and Case 3 failed due to @Versatile explanation.
Case 1 is executed because of the static member.
Let’s check the process:

In the Main method, A a = new A() row starts creating a object. In this process, since the public static B b = new B() row, an object of class B will be created – it is this line that will start creating another object of class A, because the row in the body of class B public static A a = new A(). Now, here comes the magic (without triggering circular dependencies). This line public static A a = new A() will start creating another object of class A, and when this object is created it will not create another object of class B> because it is a static member of A, and it has already been created. As we all know, static members of a class are shared across all instances of the class. Therefore, it does not trigger the creation of another object of class B. In total, we end up with three instances of the class, in order: A, B, A.

Update

Interestingly, in the first case, what happens if we initialize a static member in the constructor. Even if classes A and B declare static members, execution fails due to circular dependencies. This is because static members are not initialized before the constructor is executed.

Related Problems and Solutions