nibin012 nibin012 - 15 days ago 6
Java Question

Nested static class name starting with $

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;
}
}

Answer

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.

Comments