Forum Stats

  • 3,768,499 Users
  • 2,252,800 Discussions
  • 7,874,600 Comments

Discussions

Inner Classes

Vaishakh Menon
Vaishakh Menon Member Posts: 1
edited Feb 8, 2017 8:09AM in Java User Groups Discussions

Hi All,

I have  a doubt regarding how .class files are named when we are  dealing with anonymous classes. I know that when we create a  inner class  one  .class file is generated named Outerclassname$InnerClass.class along with the normal .class file

and if we create an anonymous class then the .class file is named OuterClass$1.class

Now check this code :

Please assume I have a Test2 Class which has a method taste();

Now I am creating this code

public class Test{    public static void main(String[] args)

    {

        Test2 a=new Test2(){          // Anonymous class is created so a .class file named Test$1 will be created

            //I am creating an another class inside anonymous class

            class MoreInner

            {

            }

            //Now again a new .class file named Test$1$MoreInner.class is created

            public void taste()

            {

                System.out.println("Spicky");

            }

        };

        a.taste();

        try{    

            System.out.println(a.getClass());

            System.out.println(a.getClass().getName());

            System.out.println(10/0); // I am creating an exception so that I can enter catch block.

        } catch(ArithmeticException e){

            //Now I am again creating a class inside Catch block

            class checker

            {

                public void eat()

                {

                    System.out.println("eat");

                }

            }

        }

    }

}

Now my Question is from the above code  4 .class files should be created  as:

1) Test.class (for outer class)

2)Test$1.class(for anonymous class)

3)Test$1$MoreInner.class ( for class created inside anonymous class)

4)Test$checker.class (for class created inside catch block)

But name of 4th .class file is Test$1checker.class. Why there is a '1' before inner class name ? why it is Test$1checker.class instead of Test$checker.class ?

Answers