Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of $ in a variable name?

Tags:

java

People also ask

What is in variable name?

A variable is a symbolic name for (or reference to) information. The variable's name represents what information the variable contains. They are called variables because the represented information can change but the operations on the variable remain the same.

What is an example of a variable name?

Variable names are shown in green color in the questionnaire Designer. The following are examples of valid variable names: age, gender, x25, age_of_hh_head. The following are examples of invalid variable names: age_ (ends with an underscore);

Which symbols can be used in variable name?

The period, the underscore, and the characters $, #, and @ can be used within variable names. For example, A. _$@#1 is a valid variable name.

Are special characters allowed in variable name?

Variable names must be unique in a Dataset. Variable names are up to 64 characters long and can only contain letters, digits and nonpunctuation characters (except that a period (.) is allowed.


$ 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.


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.