3

I compiled this code and then, TestInner$1.class emerged.

I know ~~~$1.class indicates that file has "anonymous class."

But I don't understand the reason why this class file made. I want to know the reason.

Here is the code.

public class TestInner {
    private static class Inner { }

    public static void main(String[] args){
         new Inner();
    }
}

I tried another version removed "private" identifier, like the following.

public class TestInner {
    static class Inner { }

    public static void main(String[] args){
         new Inner();
    }
}

I'd imagined that this code also would make TestInner$1.class file.

However it didn't create the file.

In addition, the following code, added Constructor, also didn't make TestInner$1.class.

public class TestInner {
    private static class Inner {
        Inner(){ }
    }

    public static void main(String[] args){
         new Inner();
    }
}

I have no idea, so can anyone help me?

EDIT:

I found the same question and it solved. Thank you for your helping.

Why is an anonymous inner class containing nothing generated from this code?

harukaeru
  • 683
  • 6
  • 15
  • 5
    You do not have any anonymous classes in your samples. – PM 77-1 Mar 03 '14 at 03:56
  • 2
    All of your samples should be creating a `TestInner$Inner.class`. Nothing should be generating `TestInner$1.class`. Perhaps it is left over from previous experiments. – Jason C Mar 03 '14 at 03:57
  • You may want to work through [Anonymous Classes tutorial](http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html). – PM 77-1 Mar 03 '14 at 04:01
  • Are you sure that you haven't used something like `new Inner(){};` by mistake? – Anthony Accioly Mar 03 '14 at 04:03
  • I do know that this code doesn't include any "anonymous class." But indeed it emerged. And "new Inner();" is not mistaken. – harukaeru Mar 03 '14 at 04:24
  • And if you doubt me, Please try to compile this code. – harukaeru Mar 03 '14 at 04:32
  • @user3161279 I did compile the code. I compiled all three of your snippets (it's the first thing I did). As expected, `TestInner$Inner.class` was produced in all 3 cases, and `TestInner$1.class` was never produced. What compiler are you using and what platform are you on? – Jason C Mar 03 '14 at 04:38
  • @Jason C Thanks a lot for your trying. I use 1.7.0_25 and Windows7. My colleague also try on 1.7.0_45 and Windows7 but he got `TestInner$1.class`, either. – harukaeru Mar 03 '14 at 04:56
  • 1
    I googled and then get the info "if you use Eclipse, this class file is not created." – harukaeru Mar 03 '14 at 05:14
  • @user3161279 Huh. Yes, I can confirm that *without* using Eclipse I get `TestInner$1.class` for your first example but not for the last two, just like you are seeing. I will have to do some more research. – Jason C Mar 03 '14 at 06:37
  • 1
    @Jason C Thank you! I'll research again, too. – harukaeru Mar 03 '14 at 07:23
  • possible duplicate of [Why is an anonymous inner class containing nothing generated from this code?](http://stackoverflow.com/questions/2883181/why-is-an-anonymous-inner-class-containing-nothing-generated-from-this-code) (as per OPs edit to question) – Jason C Mar 06 '14 at 13:59
  • @user3161279 Good find! – Jason C Mar 06 '14 at 14:01

1 Answers1

4

None of your examples have anonymous inner classes. None of them will produce a file named TestInner$1.class. All of them will produce a file named TestInner$Inner.class.

The following example shows an anonymous inner class and will produce TestInner$1.class:

public class TestInner {
    public static void main(String[] args){
         new Object() {
             @Override public String toString () {
                 return "ninja";
             }
         };
    }
}

I'm not sure where your TestInner$1.class came from but I'm guessing it's left over from previous experiments you were doing.


Update 1: I can confirm that without using Eclipse I get TestInner$1.class (in addition to TestInner$Inner.class -- 3 files are produced) for the first example but not for the last two, just like you are seeing. Will update when I find out why. When compiled via Eclipse, TestInner$1.class is never produced.


Update 2: OP found solution in Why is an anonymous inner class containing nothing generated from this code?.

Community
  • 1
  • 1
Jason C
  • 38,729
  • 14
  • 126
  • 182
  • It is impossible the file was left over absolutely. Because I'd deleted the files, TestInner*.class, and verified whether those files was really removed or not. Then I tried compiling again and again. – harukaeru Mar 03 '14 at 04:29