1

I have a question concerning the key bindings. I have the following Java code:

private void registerPressedReleasedKey(String keyChar, boolean key, boolean pressedKey) {

    // 1. decide if the key is pressed or released
    // 2. save key and its action name
    // 3. decide, what to do, when the action name is being mentioned
    // 4. change the boolean value in actionPerformed(ActionEvent ae)

    String keyStatus;

    if(pressedKey == true)
        keyStatus = "pressed ";
    else
        keyStatus = "released ";
            getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(keyStatus + keyChar), keyStatus + keyChar);
    getActionMap().put(keyStatus + keyChar, new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            key = pressedKey;
        }
    });
}

Eclipse says to me that key = keyPressed; is wrong, because I only can use final variables. My question is if there is a possibility to access and change key inside the actionPerformed(ActionEvent ae) method.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Netglex
  • 21
  • 3
  • 4
    Technically you need a final holder object for `key`. The final guarantees, that you can't assign a new reference to the holder variable, but the `final` does not prevent from updating the state of the holder object via setters. – Roman Vottner Jul 01 '15 at 07:36
  • 1
    the anonymous class code above is wrong on oh-so-many levels: a) it tries to set `key` variable, which is 1. an outer method parameter, 2. unused anywhere except as that parameter, 3. not returned nor stored anywhere, b) it's using implicitly threaded code - `getActionMap()` & `AbstractAction` suggest that a Swing component is being used, which means that `actionPerformed()` will get called by Swing thread, and the code above will probably get executed in the main thread. As such, that code is inherently faulty. The fact you can't assign `key` is the least of your problems here. –  Jul 01 '15 at 10:10
  • @RomanVottner agreed, but see my comment above. –  Jul 01 '15 at 10:10
  • @vaxquis thats why I wrote a comment and also mentioned the technically aspect - I did not put any efforts in correcting the design of the OP – Roman Vottner Jul 01 '15 at 10:38
  • possible duplicate of [Why are only final variables accessible in anonymous class?](http://stackoverflow.com/questions/4732544/why-are-only-final-variables-accessible-in-anonymous-class) –  Jul 01 '15 at 14:24

1 Answers1

2

Answering your question

It is impossible to modify external variables in an anonymous class the way you are trying to since these must be final.

If this was a field of your class, you could use access it directly (in Java > 7) or use an accessor (setter). Since it is not, the way to go would be to use a wrapper: final means you cannot assign a new value, but you can still call its methods and any accessor is basically a method.

Warning notice

I assume your code is incomplete, as in this example, you try to set the variable key, which is not used anywhere.

However, assigning a new value to a parameter is generally a bad practice.

Moreover, getActionMap() & AbstractAction suggest that a Swing component is being used, which means that actionPerformed() will get called by Swing thread, probably even after registerPressedReleaseKey() has finished. As a consequence, updating a parameter for this method makes no sense.

Community
  • 1
  • 1
Chop
  • 4,267
  • 5
  • 26
  • 58
  • @downvoter Please explain what's wrong with my answer so that I can amend it if I can and learn from my mistakes in the future. – Chop Jul 01 '15 at 10:37
  • @vaxquis Thanks, I expanded my answer to take your comments into account. – Chop Jul 01 '15 at 16:23
  • @vaxquis Thank you, I was not aware of the meta consensus on comments (though it makes sense, as most things on meta). I did not know about this specificity of Java 7 either (only beginning to use it now at work). – Chop Jul 02 '15 at 03:25
  • I couldn't find the exact discussion ATM (I got link to it by chance a couple of years ago), but it's still the case that e.g. http://meta.stackoverflow.com/questions/270105/changing-user-name-leaves-orphaned-comment-references & "All necessary information should be in the [question/answer] itself" (http://meta.stackoverflow.com/questions/266764/put-users-comment-into-his-question), also see http://meta.stackoverflow.com/questions/277922/should-comments-be-auto-deleted-from-stack-exchange-podcast-61 etc. In general, no valid info should be left *entirely* in comments. –  Jul 02 '15 at 14:32