views:

100

answers:

4

I realize that it is a valid part of a variable name, but I've never seen variable names actually use the symbol $ before.

The Java tutorial says this:

Additionally, the dollar sign character, by convention, is never used at all. You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it.

However, since this is geared toward Java beginners, I'm wondering if in the distributed world, the $ lives on with a special meaning.

A: 

I've only seen it used once intentionally, and that was for something like BigInteger $33 = BigInteger.valueOf(33L);

It looks weird.

GregS
It does look weird. Apparently, some code generators also use it. But I'm wondering if it's some convention for RMI, perhaps brought in from another language.
Thomas Owens
+1  A: 

The only time I've ever seen this is

  • in generated proxy classes, which frequently use $ in their method, variable and class names
  • inner->outer class synthetic accessor methods, which I think also use a $ in the method name
  • inner class names (as seen by the JVM)

I've never seen it used in source code, though, and I can't think of any reason for doing so. The $ is almost a definitive indicator of generated code.

skaffman
I'd have to check which classes it was in. This is in a school assignment, and the use of $ in variable names caught me off guard - I had forgotten it was even valid. If it was proxy classes, I could assume that they were probably generated.
Thomas Owens
A: 

Or an inner class.

No, I wouldn't use it in a variable name. There's nothing special about RMI to warrant such a choice, either.

duffymo
+1  A: 

However, since this is geared toward Java beginners, I'm wondering if in the distributed world, the $ lives on with a special meaning.

The advice of not using '$' in identifiers in human-written code is addressed to everyone.

The stated reason that '$' identifiers are legal but discouraged is to provide compilers and code generators a "private" extension to a identifier namespace. When a generator needs to synthesize an identifier, including a '$' in the identifier means that it will not collide with any existing (or future) identifiers in the source code-base.

If '$' was not available, the generator would need to check that each synthesized identifier does not create an identifier collision, or risk generating Java code that does not compile. This is difficult if the generated code includes Java code supplied by the programmer, and potentially impossible if the generated code needs to work with arbitrary classes that are not available to the generator at the time it is run.

I don't know the extent to which '$' is actually used by generators, but I know for a fact that the Java compiler uses them for names of nested and anonymous inner classes, and (I think) related hidden variables. So you should pay attention to this advice.

Stephen C