1

Sometimes I met such expressions as aClass$field instead of aClass.field. What does it mean in java? Maybe it is a primitive question, but it is impossible to search the web for $. And surely it would give non-programming answers. So, please, help.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
  • @skaffman No, thank you. The question, mentioned by you, is a much more elaborated question about the use of the subject. It won't help me, I am afraid. I am asking the most simple question - what is it a$b? – Gangnus Jan 30 '12 at 11:51

2 Answers2

4

The $ file means, according to the JLS:

The $ character should be used only in mechanically generated source code or, rarely, to access preexisting names on legacy systems.

The $ on the class file states that (e.g. AClass$BClass.class) BClass is nested inside AClass.

A.B is only on code level while A$B is on generated class level.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
2

$ has no special meaning in Java. It can be used to name every variable or method, also as the first (or only) character. In example, the following class would be perfectly legal.

public class Test {

    private int $;

    private int $field;

    private int my$field;

    private void $() {
    }

    private void $method() {
    }

    private void my$method() {
    }

}

However you can't use $ to name a class. When the Java compiler encounters an inner class B inside a class A, the resulting .class file will be named A$B.class. So naming a class using a $ character will result in a compile error.

frm
  • 3,346
  • 1
  • 21
  • 21
  • Thank you for the understandable answer, just on the level of my primitive question. BTW: in tabs of Eclipse there is no .class suffix. Is it simply shortening or it has some deeper sense? – Gangnus Jan 30 '12 at 11:59
  • 1
    Tabs in Eclipse will show you the logical name of the class. If you are editing a `Test.java` file with a `Test` class inside it, Eclipse will show you only `Test`, the class name. The `Test` class, in the end, will compile to a `Test.class` file. You can see the `.java` and `.class` file using the _Package explorer_ or _Navigator_ views. – frm Jan 30 '12 at 12:08