0

I expect the output of the below program to be:

Inside Static Block A
Inside A
Inside Constructor B

But the output is:

Inside Static Block A
Inside A
Inside A
Inside A
.
.
.
Inside A(Infinite times)

The code is:

public class First
{
    public static void main(String args[])
    {
        A op=new A();
    }
}
class A
{
    private int a=100;
    private int b;
    A()
    {
        System.out.print("Inside A");
        B obj=new B();
    }
    static
    {
        System.out.print("Inside Static Block A");
    }
    class B
    {
        B()
        {
            System.out.print("Inside Constructor B");
        }
        A o=new A();
    }
}

Can someone please tell me why the output is so?

1
  • 1
    B obj=new B(); and A o=new A(); - what do you think this does? Commented Jun 28, 2015 at 19:33

4 Answers 4

3

You have a circular reference between B and A. In the constructor of A you create a B and in B you create a field with an A.

In the constructor of A() you do:

B obj = new B();

so, you create a new B().

In B() you create a field with an A(). This creates another instance of A() which again creates a B() in its constructor, and so forth.

Sign up to request clarification or add additional context in comments.

6 Comments

Can you please elaborate? I have called the constructor A after constructor B block,then how it is able to call it ...
No @Kai Sternad, I have called the constructor A from outside the constructor B block
@Raj assigning a value to a field when you declare it is effectively like code which is prepended to the constructor. You are still invoking it whenever you create the instance of B.
@RajMalhotra the constructor of B doesn't just return because the fields are also initialized. And that's where the problem is.
@Raj field assignments happen before the constructor is called, in order that you could potentially use the field values in the ctor.
|
1

It is due to the fact that you have a circular reference between Class A and Class B. In your constructor of A you, create an object B

 A()
 {
   System.out.print("Inside A");
    B obj=new B(); // You are creating a new object of Class B
 }

In Class B you are creating a new object of class A

 class B
 {
     B()
     {
        System.out.print("Inside Constructor B");
     }
     A o=new A(); 
 }

Here you create an object of class A which again calls the constructor of Class A which creates an object of Class B again and hence the whole process goes into an infinite loop

Comments

1

Take a look at

A() {
    System.out.print("Inside A");
    B obj = new B();
}

So to create instance of class A you need to create instance of class B.

But code of class B is

class B {
    B() {
        System.out.print("Inside Constructor B");
    }

    A o = new A();
}

which (since initialization of fields happens at start of each constructors) is similar to as

class B {
    B() {
        o = new A();
        System.out.print("Inside Constructor B");
    }

    A o = null;
}

so as you see it also needs to create instance of class A.

So when you call

public static void main(String args[])
{
    A op=new A();
}

new A() first loads A class which executes

static {
    System.out.print("Inside Static Block A");
}

and then tries to execute constructor A(){

System.out.print("Inside A");
B obj = new B();

but before it ends its execution it calls new B() which calls code from constructor B(){ which as already shown is

o = new A();
System.out.print("Inside Constructor B");

and we and up with having to create another instance of A even before we are able to print Inside Constructor B.

So as you see to create instance of class A you need to create instance of class B which needs to create another instance of class A which needs to create another instance of class B ... and so on until stack will overflow.

Comments

0

The point is not accessible due recursive calls.

B()
{
    System.out.println("Inside Constructor B");
    A o=new A();
}

A o=new A();

This code should return desired output:

Inside Static Block A
Inside A
Inside Constructor B

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.