-1
class Outer
 {
   int outer_x = 100;
    void test()
     {
       for(int i=0; i<10; i++)
        {
          class Inner
            {
                void display() 
                    {
                       System.out.println("display: outer_x = " + outer_x);
                     }
              }
           Inner inner = new Inner();
           inner.display();
         }
      }
 }

How many times Inner class will be created when i call test() ,is it ten times or only once and 10 object are created?Also tell how you came up with this conclusion.

Shubham Tyagi
  • 181
  • 1
  • 3
  • 14

2 Answers2

1

The class Inner is created only once, when your code is compiled, as @EJP commented. You can easily verify this when looking at the compilation output.

I created a .java file called Outer.java and simply compiled it. The result was two .class files.

Compilation output

You can see that the compiler created a class file called Outer.class but also a class file called Outer$1Inner.class, which is simply your local class. Here, Java uses $1 to mark the inner anonymous classes.

Adding a main method and calling new Outer().test(); you can see that the output is indeed 10 lines, which means 10 instances of Inner were created. You can easily verify this by adding a hashCode() somewhere to the output like so

System.out.println("display: outer_x = " + outer_x + " --> " + hashCode());

Which gives the following output:

display: outer_x = 100 --> 1704856573
display: outer_x = 100 --> 705927765
display: outer_x = 100 --> 366712642
display: outer_x = 100 --> 1829164700
display: outer_x = 100 --> 2018699554
display: outer_x = 100 --> 1311053135
display: outer_x = 100 --> 118352462
display: outer_x = 100 --> 1550089733
display: outer_x = 100 --> 865113938
display: outer_x = 100 --> 1442407170

Different hashcodes mean different instances.

Now all this happens, because the compiler doesn't care about control flow statements like for when it comes to collecting classes. It sees the inner class and compiles it once. The output on the other hand happens at run time and follows control flow statements.

QBrute
  • 4,405
  • 6
  • 34
  • 40
0

The class is only created once. As well as the implementation detail of being able to see the generated class files, you can show that it's the same Class object by printing the class's id:

    for (int i = 0; i < 10; i++) {
        class Inner {

            void display() {
                System.out.println("display: outer_x = " + outer_x);
            }
        }
        Inner inner = new Inner();
        System.out.println(System.identityHashCode(inner.getClass()));
        inner.display();
    }

This prints the same number each time.

But what if you coded it such that each time around the loop you made a class that behaves differently?

    for (int i = 0; i < 10; i++) {
        class Inner {

            private int field = i;               

            void display() {
                System.out.println("My field = " + field);
            }
        }
    }

This won't compile, because Java will only create one class, so it must be the same each time.

The compilation error is:local variables referenced from an inner class must be final or effectively final, since i is not final, nor can the compiler treat it as if it is final. If the variable is final, then the class will be identical each iteration.

slim
  • 40,215
  • 13
  • 94
  • 127