问题
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.
回答1:
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.
回答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
, anda-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.
回答3:
I've only seen it used once intentionally, and that was for something like BigInteger $33 = BigInteger.valueOf(33L);
It looks weird.
回答4:
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.
回答5:
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 :(
来源:https://stackoverflow.com/questions/1987603/is-there-a-convention-when-using-java-rmi-to-use-the-dollar-sign-in-a-variab