In what order do local and anonymous classes compile in java?

Viewed 90

Assume that I have two local classes with the same names, but they're defined in different methods. In what order will they compile and what names are they going to have?

class ExampleClass {

    public void test() {
        NestedClass.nestedTest2();
        NestedClass.nestedTest();

    }

    private static class NestedClass {
        private static int a;

        public static void nestedTest() {
            class Test1 {
                void method1() {}

            }
        }

        public static void nestedTest2() {
            class Test1 {
                void method2() {}
            }
        }
    }
}

Using javac command I get these compiled files

ExampleClass$NestedClass$1Test1
ExampleClass$NestedClass$2Test1

There are other files, I just don't really thing it's necessary to list them all.

The question is, how come the first file (ending with $1Test1, which is defined in nestedTest() method ) get compiled earlier than the other one (defined in method nestedTest2)? What does the order depend on? As you see I also tried to call test methods in ExampleClass to change the order, but I didn't believe it would help and it actually doesn't.

0 Answers
Related