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
Knute Johnson - 01 May 2006 02:23 GMT
> Hi,
>
[quoted text clipped - 26 lines]
>
> Aron
That doesn't work though does it?

Signature
Knute Johnson
email s/nospam/knute/
timasmith@hotmail.com - 01 May 2006 21:35 GMT
sure it does
Knute Johnson - 02 May 2006 18:25 GMT
> 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?

Signature
Knute Johnson
email s/nospam/knute/
Timo Stamm - 02 May 2006 18:41 GMT
Knute Johnson schrieb:
>> 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?
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
hiwa - 01 May 2006 03:59 GMT
> 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.
Thomas Hawtin - 01 May 2006 07:37 GMT
> I have the following code which complains with 'Cannot refer to a
> non-final variable controlPanel inside an inner class defined in a
[quoted text clipped - 8 lines]
> 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

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/