Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the $ sign in Java? Please have a look in the Java code below

Tags:

java

I've searched everywhere trying to figure out what is the val$editorkit or the $ sign below means, but no luck...please help...

private synchronized void updateHtmlEditor(HTMLEditorKit editorkit, StringReader reader)
  {
    Runnable runnable = new Runnable(editorkit, reader)
    {
      public void run() {
        try {
          this.val$editorkit.read(this.val$reader, LinkParser.this.htmlViewEditor.getDocument(), LinkParser.this.htmlViewEditor.getDocument().getLength());

        } catch (IOException ex) {
          Logger.getLogger(LinkParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BadLocationException ex) {
          Logger.getLogger(LinkParser.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
    };
    SwingUtilities.invokeLater(runnable);
  }
like image 597
scamexdotexe Avatar asked Feb 04 '12 16:02

scamexdotexe


People also ask

What is this symbol (&) in Java means?

The & operator in Java has two definite functions: As a Relational Operator: & is used as a relational operator to check a conditional statement just like && operator. Both even give the same result, i.e. true if all conditions are true, false if any one condition is false.

What is this and this () in Java?

In Java, both this and this() are completely different from each other. this keyword is used to refer to the current object, i.e. through which the method is called. this() is used to call one constructor from the other of the same class.


4 Answers

It doesn't mean anything special - it's just a letter that forms part of the name, exactly like the l before it or the e afterwards.

See the JLS section on Identifiers for full details about what is and isn't allowed in a name.

like image 66
Andrzej Doyle Avatar answered Oct 05 '22 23:10

Andrzej Doyle


We can't really tell I guess since you did not provide the variable declaration, but in Java the $ can be a part of a variable name, for instance you can do something like so:

String str$rr = "Hello";
System.out.println(str$rr);

Something like that will print Hello.

like image 22
npinti Avatar answered Oct 05 '22 23:10

npinti


The $ sign is used by the Java compiler for generated names of inner classes, synthetic fields and methods. It is valid for identifiers in Java source, but discouraged.

The code you show looks like decompiled code of an anonymous inner class. The anonymous Runnable implementation within the method updateHtmlEditor accesses the parameters of its surrounding method. In order to make that access possible, the parameters need to be declared final. In Java byte code, the anonymous class has three final instance attributes, this$0 containing the outer instance LinkParser.this, val$editorkit and val$reader containing the outer method parameters, and a constructor with three arguments, which assigns its arguments to the attributes.

Note also that LinkParser.this.htmlViewEditor is a reference to an attribute of the outer class, LinkParser. In this sample, the explicit reference to the outer instance LinkParser.this can be omitted.

The original source code looks something like this:

private synchronized void updateHtmlEditor(final HTMLEditorKit editorkit, final StringReader reader)
  {
    Runnable runnable = new Runnable()
    {
      public void run() {
        try {
          editorkit.read(reader, htmlViewEditor.getDocument(), htmlViewEditor.getDocument().getLength());
        } catch (IOException ex) {
          Logger.getLogger(LinkParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BadLocationException ex) {
          Logger.getLogger(LinkParser.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
    };
    SwingUtilities.invokeLater(runnable);
  }
like image 41
Christian Semrau Avatar answered Oct 06 '22 01:10

Christian Semrau


It is allowed to have the dollar sign in variable names but by convention it is not used. have a look here http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

like image 20
redben Avatar answered Oct 06 '22 00:10

redben