When should I use the dollar symbol ($) in a variable name?
Your question is tagged Java, but it looks like you're asking about type characters, which are still supported in Visual Basic but not widely used these days (nor for a long time).
This is a bit subjective, but I think it's fair to say: never
One scenario where you might want to prefix a variable name with $
is when writing JavaScript code to distinguish jQuery objects.
Edit
Regarding starting a variable name with a $
, the Oracle Java tutorials tell us:
A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "
$
", or the underscore character "_
". The convention, however, is to always begin your variable names with a letter, not "$
" or "_
".
Name conflicts with synthetic fields
McDowell quoted the JLS, here is an example that fails to compile on Oracle JDK 1.8.0_45 due to name conflicts with synthetic fields:
public class Assert {
static final boolean $assertionsDisabled = false;
public static void main(String[] args) {
assert System.currentTimeMillis() == 0L;
}
}
The problem is that javac
generates a field $assertionsDisabled
to implement assert
, which conflicts with our field, and javac
says:
the symbol $assertionsDisabled conflicts with a compile synthesized symbol
See also: What does the Java assert keyword do, and when should it be used?
From the Java Language Specification on identifiers:
The
$
character should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.
Theoretically you can use dollar sign in variable names, but it's strongly discouraged because that symbol is used internally by the compiler(e.g. inner or anonymous classes's name).
Please refer to two related questions on the stackoverflow: What is the meaning of $ in a variable name? and Java class name containing dollar sign fails to compile if an inner class is present