61

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.

4
  • Related thread : stackoverflow.com/questions/6167326/… Sep 20, 2011 at 11:19
  • Citing where you read this might be nice.
    – Don Roby
    Sep 20, 2011 at 11:22
  • AFAIK there's no special meaning, so you can use it in your variable declarations. But it violates Java Naming Conventions. Maybe he's talking about class names? Sep 20, 2011 at 11:29
  • 2
    @MisterSmith I suspect the "special meaning" referred by the author is just the convention of only using it on machine-generated code. The "special meaning" is special by convention, not by semantics.
    – brandizzi
    Sep 20, 2011 at 12:57

2 Answers 2

62

$ is used internally by the compiler to decorate certain names. Wikipedia gives the following example:

public class foo {
    class bar {
        public int x;
    }

    public void zark () {
        Object f = new Object () {
            public String toString() {
                return "hello";
            }
        };
    }
}

Compiling this program will produce three .class files:

  • foo.class, containing the main (outer) class foo
  • foo$bar.class, containing the named inner class foo.bar
  • foo$1.class, containing the anonymous 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, javac uses $ in some automatically-generated variable names: for example, this$0 et al are used for the implicit this references from the inner classes to their outer classes.

Finally, the JLS recommends the following:

The $ character should be used only in mechanically generated source code or, rarely, to access preexisting names on legacy systems.

3
  • That's not really true. I can name my class Test$ Sep 20, 2011 at 11:18
  • 5
    The question is not asking for class names, but for variables. Sep 20, 2011 at 11:24
  • What would happen if there was a class named foo$bar? How would its class file be distinguished from that of bar in foo in the source code?
    – matj1
    Apr 7, 2022 at 17:44
39

There is no special meaning for a $ in a variable’s name.

While technically allowed, starting the variable name with a dollar sign goes against convention, generally used only by code-generators.

To quote the Java Tutorial by Oracle:

Variable names are case-sensitive. 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 "_". 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. A similar convention exists for the underscore character; while it's technically legal to begin your variable's name with "_", this practice is discouraged. White space is not permitted.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy