简体   繁体   中英

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

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);
  }

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.

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 .

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);
  }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM