What is "val$title" (dollar sign in expression) in Java?

Go To StackoverFlow.com

5

private void launchEventPanel(String title) {
    EventQueue.invokeLater(new Runnable(title) {
        public void run() {
            JFrame myFrame = new JFrame();
            myFrame.setTitle(this.val$title);
            myFrame.setIconImage(CrConference.this.mainCore.myPanel.myIconManager.getPromptIcon(Mart.class.toString()));
            myFrame.getContentPane().add(Conference.this.myEventPanel, "Center");
            myFrame.pack();
            myFrame.setVisible(true);
        }
    });
}

i got some code that i am trying to compile and understand. help highly appreciated

2012-04-05 18:18
by java-learner
http://stackoverflow.com/questions/65475/valid-characters-in-a-java-class-name , http://stackoverflow.com/questions/5845299/why-is-it-bad-to-start-a-variable-name-with-a-dollar-sign-in-c-java-and-simila , http://stackoverflow.com/questions/1987603/is-there-a-convention-when-using-java-rmi-to-use-the-dollar-sign-in-a-varia - NoName 2012-04-05 18:29
Is this decompiled code? I would expect variable names like that in decompiled code - Jesper 2012-04-05 18:30
I have edited the title to reflect the "odd code", val$title a perfectly valid Java identifier -- it's just as valid as foobar -- but the "rule" is to not use it (except tools that generate code automatically) - NoName 2012-04-05 18:31


3

This line:

myFrame.setTitle(this.val$title);

Is simply setting the title of a JFrame object, using the value of the attribute val$title for doing so. val$title is an instance attribute of the current class, its name is a bit unusual (because of the $) but nevertheless, valid for an identifier in Java.

2012-04-05 18:21
by Óscar López
It could be a local variable : - Eng.Fouad 2012-04-05 18:24
@Eng.Fouad no local variables with that name are declared in the code in the questio - Óscar López 2012-04-05 18:28
oh, you are right. I didn't notice that - Eng.Fouad 2012-04-05 18:30
thanks for the help guy - java-learner 2012-04-06 17:55
@user1315911 you're welcome! if this answer was helpful for you, please consider accepting it (click the check mark to its left - Óscar López 2012-04-06 18:22


4

As described here and here, the argument to the Runnable constuctor and "this.val$" to the field name is added by the compiler and shows up in the generated bytecode. Hence these extra things are reflected in the decompiled code.

To get the original decompiled code, add final to the declaration of title and remove title from the call to Runnable and the this.val$ from in front of title:

private void launchEventPanel(final String title) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            JFrame myFrame = new JFrame();
            myFrame.setTitle(title);
            myFrame.setIconImage(CrConference.this.mainCore.myPanel.myIconManager.getPromptIcon(Mart.class.toString()));
            myFrame.getContentPane().add(Conference.this.myEventPanel, "Center");
            myFrame.pack();
            myFrame.setVisible(true);
        }
    });
}
2012-11-27 18:21
by Tim Lewis


2

line 5 is just setting the title of the frame (the text you see on the top of the window frame in windows) "this.val$title" is just a local memeber named val$title that whoever wrote the code stored the title string in.

While it is a bit uncommon to see most languages based on C treat $ as an alphapetic character, like a-z or A-Z.

2012-04-05 18:21
by tletnes
Languages loosely based on C, or that use C-like syntax, maybe. C itself doesn't, nor does C++ AFAIK. JS and Java are the only C-alikes i can think of of that do, actually - cHao 2012-11-27 18:23
Ads