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

I searched everywhere, trying to figure out what val $ editorkit or the $ sign is below, 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); } 
+7
source share
4 answers

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

The code you show looks like decompiled code of an anonymous inner class. The anonymous Runnable implementation in the updateHtmlEditor method accesses the parameters of its surrounding method. To make access available, parameters must be declared final . In Java byte code, an anonymous class has three attributes of the final instance, this$0 , which contains the external instance of LinkParser.this , val$editorkit and val$reader , which contains the parameters of the external method, and a constructor with three arguments that assigns its arguments attributes.

Note that LinkParser.this.htmlViewEditor is a reference to an attribute of the external LinkParser class. In this example, an explicit reference to an external instance of LinkParser.this may be omitted.

The 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); } 
+3
source

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

For more information, see the JLS section in Identifiers for detailed information about what is and is not allowed in the name.

+15
source

We cannot say, I think, since you did not specify a variable declaration, but in Java $ can be part of the variable name, for example, you can do something like this:

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

Something like this will print Hello .

+4
source

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

+1
source

All Articles