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
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