$ in a variable name in Java

0 votes

I am a beginner in Java. While looking at the naming conventions for variables it was mentioned that we can use "$" symbol in the variable name. Along with it, there was this line which said 

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

So can someone explain what is the special meaning behind $ symbol?

Oct 9, 2018 in Java by 93.lynn
• 1,570 points
35 views

1 answer to this question.

0 votes

Java compiler uses "$" symbol internally to decorate certain names.

For Example:

public class demo {
    class test {
        public int x;
    }

    public void myTrial () {
        Object obj = new Object () {
            public String toString() {
                return "hello world!";
            }
        };
    }
}

Compiling this program will produce three .class files:

  • demo.class, containing the main (outer) class demo
  • demo$test.class, containing the named inner class demo.test
  • demo$1.class, containing the anonymous inner class (local to method demo.myTrial)

The javac uses $ in some automatically-generated variable names for the implicitly referencing from the inner classes to their outer classes.​

answered Oct 9, 2018 by code.reaper12
• 3,450 points

Related Questions In Java

0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20 in Java by Sirajul
• 35,820 points
183 views
0 votes
2 answers

How to create a 2-D array in java?

int[][] multi = new int[5][]; multi[0] = new ...READ MORE

answered Jul 16, 2018 in Java by Daisy
• 8,050 points
55 views
0 votes
3 answers

How to read a Text File in Java?

You can use readAllLines and the join method to ...READ MORE

answered Jul 27, 2018 in Java by samarth295
• 2,190 points
109 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,050 points
800 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 10,028 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,110 points
2,258 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,110 points
5,881 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,110 points
683 views
0 votes
1 answer

Deep copying a 2d array in Java

I guess, for deep copying you will ...READ MORE

answered Nov 12, 2018 in Java by code.reaper12
• 3,450 points
466 views
0 votes
1 answer

Purpose of “String args[]” in the “psvm” of Java

Let me give you the complete explanation ...READ MORE

answered May 7, 2018 in Java by code.reaper12
• 3,450 points
103 views