Is there a convention, when using Java RMI, to use the dollar sign $ in a variable name?
Asked Answered
C

5

1

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.

Colum answered 1/1, 2010 at 0:49 Comment(1)
If you'd asked me, I'd have guessed that $ was valid in class files, but not java source.Zindman
Z
3

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.

Zindman answered 1/1, 2010 at 0:54 Comment(1)
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.Colum
V
2

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

The Tutorial's advice to not use '$' in identifiers in human-written code is addressed to everyone.

Indeed, the JLS says roughly the same thing, and that document is most decidedly NOT aimed at beginners:

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

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

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.

Ventricular answered 1/1, 2010 at 2:53 Comment(0)
G
0

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

It looks weird.

Guarnerius answered 1/1, 2010 at 0:53 Comment(1)
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.Colum
K
0

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.

Kell answered 1/1, 2010 at 0:58 Comment(0)
M
0

If you want to see real-life (probably) human-written code making extensive use of '$' for variable naming, you can have a look at PacketWorld (a simulation tool for multi-agent systems).

I just started working with it and was sent here after a google search on 'Why use dollar in java?'.

Still doesn't make sense :(

Mathildemathis answered 26/5, 2011 at 10:42 Comment(1)
What can you say? There's always someone who thinks that the rules don't apply to him. Sigh.Ventricular

© 2022 - 2024 — McMap. All rights reserved.