Why I cannot refer to a non-final variable inside an inner class defined in a different method?
I've seen topics about this and in most of them people say that your component should be final and ... . But nobody says why?!! and I don't know what's the philosophy behind this limitation.
What makes me confused more is that the following code is erroneous :
JButton removeJBtn = new JButton("Remove");
JButton addJBtn = new JButton("Add");
//...
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
removeJBtn.disable();//Error here,Cannot ...
}
});
while if I define
JButton removeJBtn
as a member field (in the body of the class,not the method) it's not required to be defined as final!
I'll really appreciate any logical answer to this daily limit that I (and probably many others) always face.
Dear users that marked this question as a duplicate, please at least give a reference to the original question (that has been answered for sure!), there is a link in added above my question that I read it throughly but it's full of contradictions, some one (with 88 up votes) says java captures the value of final variables and it's completely rejected by a comment (with 16 votes) below it.