views:

76

answers:

1

I have a class A. I have a reference ref of class A pointing to an object of type x. What kind of object makes ref.getClass() print A$1 ? And what does $ signify?

+13  A: 

The $ signifies an inner class. In this case:

public class A {
  public A() {
    Runnable r1 = new Runnable() {
      public void run() { ... }
    };
  }

  private static class Inner {
    ...
  }
}

The Runnable inside the constructor will result in a class file A$1.class and the Inner class will create a file called A$Inner.class.

Anonymous inner classes are sequentially numbered from 1 as they are encountered (although I'm not sure this behaviour is guaranteed or not). Named inner classes append their name after the $.

cletus