开发者

what is the signficance of the Java class files of the sort myClass$1.class?

I have a number of .java classes compiled with Eclipse and over in the /bin directory I see that not only do I have various .class files corresponding to my Java c开发者_运维技巧lasses but also a few with a dollar sign in the file name.

Example: I have a class called RangeFinder and in the /bin I see a RangeFinder.class and also a RangeFinder$1.class.

What is the significance of the latter?

(I am on Ubuntu and I am using Eclipse EE Indigo.)


These are anonymous inner classes in bytecode form. The compiler gives them numerical names starting with 1 (it is not allowed in Java to have a class name starting with a number, but it is possible in the bytecode, so the compiler does it to avoid name clashes, I guess). Normal (named) inner classes are named like OuterType$InnerType.class.


Each .java source file can contain definitions for multiple Java classes. However, every .class file can only contain the bytecode for a single class.

So, if you have a file Foo.java that looks like this:

public class Foo {
    public class Inner {
        …
    }

    public void method() {
        widget.addListener(new Listener() {
            public void listen() {…}
        }
    }
}

class Bar {
    …
}

It will compile into the following files:

  • Foo.class
  • Foo$Inner.class
  • Foo$1.class
  • Bar.class

Anonymous classes are assigned numbers as "names", I believe in the order in which they're found in the enclosing class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜