Frosted Cupcake
Frosted Cupcake

Reputation: 1970

Non-static nested class in java

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?

Upvotes: 0

Views: 116

Answers (4)

Pshemo
Pshemo

Reputation: 124215

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.

Upvotes: 1

Sneh
Sneh

Reputation: 3737

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

Upvotes: 1

Kai Sternad
Kai Sternad

Reputation: 22830

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.

Upvotes: 3

Roman C
Roman C

Reputation: 1

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

Upvotes: 0

Related Questions