-1

I have seen some codebases where people have wrote nested static classes with names starting with "$" ? What is the significance of that ?

class A {
  int a;

  static class $B {
    int b;  
  }
} 
nibin012
  • 1,793
  • 2
  • 15
  • 14

3 Answers3

6

There is no significance. $ is a valid character for a java name; there's nothing special about it.

JLS Chapter 3.8, which defines valid class name characters (more-or-less most "non syntax" characters, first char not a digit) even specifically cautions against this very situation:

The $ sign should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems

See JLS Chapter 6: Names too.

This is just a local naming convention, perhaps to remind/indicate to developers that the class is an inner class.

Although it's a style thing, AFAICT the general consensus of the dev community is to not encode meta data into a name. Other examples I have seen are leading underscores for member variables/parameters, and the awful Hungarian notation.

Try to give things good undecorated names.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
2

People generally haven't written them themselves; those are usually autogenerated somewhere. From JLS 3.8:

The "Java letters" include uppercase and lowercase ASCII Latin letters A-Z (\u0041-\u005a), and a-z (\u0061-\u007a), and, for historical reasons, the ASCII underscore (_, or \u005f) and dollar sign ($, or \u0024). The $ sign should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

The dollar sign is a legitimate character, but it is a very strong convention, on par with using capital letters for class names, not to use it in ordinary code.

A handful of people use it to indicate an inner class, but I personally have never once seen the $ character used for anything that did not also carry the synthetic flag, and I would consider it on par with using Hungarian notation in Java.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
2

What is the significance of that ?

You would have to ask the person who wrote the code what the significance is.

However, there is no significance from the Java language perspective; i.e. the Java compiler places no special meaning on the dollar sign. (It is just another legal identifier character, albeit one that you are recommended not to use; see JLS 3.8).

However, I suspect that the reason that the dollar character is that:

  • The code has at some point been decompiled from bytecodes. I say this because the Java compiler uses A$B as that synthetic name for a nested class B declared in A.
  • The person / people who have modified / maintained it since then either don't know about or don't care about standard Java style conventions.

It could also be a (deliberate) "local convention".

Either way, most Java programmers would find this objectionable. Don't copy it.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216