Ajay
Ajay

Reputation: 7428

Classname + $ . What causes it?

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?

Upvotes: 1

Views: 137

Answers (1)

cletus
cletus

Reputation: 625087

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 $.

Upvotes: 13

Related Questions