3

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 classes 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.)

H2ONaCl
  • 10,644
  • 14
  • 70
  • 114
  • 2
    It's an anonymous inner class. See http://stackoverflow.com/questions/380406/java-inner-class-class-file-names – NPE Aug 02 '11 at 14:05

2 Answers2

3

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.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • the inner class name does not start w/ a number though. And having class name org.yyy.XXX$1 is valid, no clashes w/ bytecode. inner classes are absolutely normal classes for the verifier and the JIT/JVM. The only difference is the metadata (inner/outer info) that's not used by the runtime itself. – bestsss Aug 02 '11 at 14:13
  • @bestsss, for the sake of precision, what I meant is that code like `class 1 { }` is not valid Java, regardless of whether it is at outer level or inside another class. – Péter Török Aug 02 '11 at 14:23
  • *for the sake of precision, what I meant is that code like class 1 { }* i know you did – bestsss Aug 02 '11 at 14:44
2

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.

millimoose
  • 39,073
  • 9
  • 82
  • 134