final variables as parameters due to action listeners

T

timasmith

Hi,

I have the following code which complains with 'Cannot refer to a
non-final variable controlPanel inside an inner class defined in a
different method':

private void addPanelListeners(PanelAddRemove controlPanel) {
controlPanel.getPrintButton().setText("Move up");
controlPanel.getPreviewButton().setText("Move down");
controlPanel.getPrintButton().addActionListener(new
MenuActionListener(frame) {
public void actionExecuted(ActionEvent e) throws Exception {
int position = controlPanel.getSelectedIndex();
}
});
}

If however I change the parameter to be final

private void addPanelListeners(final PanelAddRemove controlPanel) {

it no longer complains. I must confess I used to create a private
variable in the class as a work around which worked but was ugly. Now
are there any detrimental side effects to making the object final - I
assume it means dont point controlPanel to another value - but
otherwise it is fine?

thanks

Aron
 
K

Knute Johnson

Hi,

I have the following code which complains with 'Cannot refer to a
non-final variable controlPanel inside an inner class defined in a
different method':

private void addPanelListeners(PanelAddRemove controlPanel) {
controlPanel.getPrintButton().setText("Move up");
controlPanel.getPreviewButton().setText("Move down");
controlPanel.getPrintButton().addActionListener(new
MenuActionListener(frame) {
public void actionExecuted(ActionEvent e) throws Exception {
int position = controlPanel.getSelectedIndex();
}
});
}

If however I change the parameter to be final

private void addPanelListeners(final PanelAddRemove controlPanel) {

it no longer complains. I must confess I used to create a private
variable in the class as a work around which worked but was ugly. Now
are there any detrimental side effects to making the object final - I
assume it means dont point controlPanel to another value - but
otherwise it is fine?

thanks

Aron

That doesn't work though does it?
 
H

hiwa

I used to create a private
variable in the class as a work around
It shouldn't be called 'work around'.
If it is safe to be unassigned a value or the value being
changed in the inner class, then you shuld use class
field, not lacal var or method arg.
 
T

Thomas Hawtin

I have the following code which complains with 'Cannot refer to a
non-final variable controlPanel inside an inner class defined in a
different method':

[...]

If however I change the parameter to be final

private void addPanelListeners(final PanelAddRemove controlPanel) {

it no longer complains. I must confess I used to create a private
variable in the class as a work around which worked but was ugly.

Erm yes, bad idea...
Now
are there any detrimental side effects to making the object final - I
assume it means dont point controlPanel to another value - but
otherwise it is fine?

It's technically the reference which is final, not the object itself.
It's fine. Some people make all local variables final unless they need
to be mutable. I think it would have been better to make final the default.

Tom Hawtin
 
K

Knute Johnson

sure it does

That has some interesting implications. What would happen if you
changed the original reference? Probably nothing. Has anybody seen
docs anywhere that say not to do it that way?
 
T

Timo Stamm

Knute said:
That has some interesting implications. What would happen if you
changed the original reference? Probably nothing. Has anybody seen
docs anywhere that say not to do it that way?

No. I think this is perfectly fine.

There are no implications, except that you cannot change the reference
anymore. IMHO, changing an method argument is bad style. I agree with
Tom that "final" should be default.

The web framework Wicket (wicket.sourceforge.net) uses anonymous classes
very extensively, and making method arguments final is often necessary,
even more often than in Swing. It doesn't cause any problems.

AFAIK, IDEA can automatically insert "final" for method arguments.


Timo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top