Zignd
Zignd

Reputation: 7025

When should I use the dollar symbol ($) in a variable name?

The dollar symbol ($) is a valid character to name a variable, e.g. String superSecretFormula$;, but when we're talking about naming conventions, when should I use this symbol?

Underscore for example is mostly used to separate words, as blank spaces can't be used.

Upvotes: 34

Views: 48400

Answers (6)

LEMUEL  ADANE
LEMUEL ADANE

Reputation: 8818

In Java, I use the "_" as a prefix to a private class-wide non-static variable or fields and "$" as a prefix to a private class-wide static variable.

It's better than using the "m" prefix because it does not block the line of reading. You obviously would not begin to read the "_" or the "$" when you begin to read the variable word, right? Like _addressMap or $addressMap but with m like in Map mAddressMap it can be confusing at the point you are beginning to read it.

Although it is not conventional Java code, it's a great help to mark those variables so you can understand them promptly if they are class-wide or not, or static or non-static. Of course you must discuss this to your teammate at first hand.

Upvotes: 1

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?

Upvotes: 5

Richard Ev
Richard Ev

Reputation: 54117

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 "_".

Upvotes: 13

Pablo R. Mier
Pablo R. Mier

Reputation: 747

I agree that, in general, the use of the special symbol $ should be avoided, as explained by Richard. However, I remember seeing the use of $ to define a jQuery-like selector in the reactor project. I think that, used with caution, the $ symbol can help to define some beautiful APIs in Java.

Upvotes: 2

Hui Zheng
Hui Zheng

Reputation: 10224

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

Upvotes: 18

McDowell
McDowell

Reputation: 108899

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.

Upvotes: 48

Related Questions