Recently I read that the sign $ is allowed in Java variable names, but has a special meaning. Unfortunately it isn’t mentioned what this special meaning is.
Therefore I ask here: What is the special meaning of $ in variable names in Java?
Here is the exact quote from
Java: An Introduction to Problem Solving and Programming
from Walter Savitch:
Java does allow the dollar sign symbol $ to appear in an identifier,
but these identifiers have a special meaning, so you should not use
the $ symbol in your identifiers.
$is used internally by the compiler to decorate certain names. Wikipedia gives the following example:Compiling this program will produce three .class files:
foo.class, containing the main (outer) classfoofoo$bar.class,containing the named inner class
foo.barfoo$1.class, containing theanonymous inner class (local to method
foo.zark)All of these class names are valid (as
$symbols are permitted in the JVM specification).In a similar vein,
javacuses$in some automatically-generated variable names: for example,this$0et al are used for the implicitthisreferences from the inner classes to their outer classes.Finally, the JLS recommends the following: