Results 1 to 15 of 15
Thread: anon inner class
- 05-08-2012, 06:13 PM #1
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
anon inner class
public class MyApp2{
public MyApp2(){
JFrame frame = new JFrame ("My App");
JTextField tf = new JTextField();
JButton button = new JButton("ok");
button.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
System.out.println(tf.getText());
}
}
);
}
}
Assume everything has been imported etc.
Can someone explain why the textfield tf has to be declared as final, I don't understand why.
Thanks for your time
- 05-08-2012, 06:28 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: anon inner class
This ensures the reference tf does not change, and so is not accidentally reflected in the inner class.
Something along the lines of:
It's possible that the action listener could be referring to the wrong text field.Java Code:JTextField tf = new JTextField(); myPanel.add(tf); JButton button = new JButton("ok"); button.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ System.out.println(tf.getText()); } } ); tf = new JTextField(); myPanel.add(tf);
So the desgners way back when in the 90s decided to avoid that whole question and simply insist that these references are final.
I wish I had the documentation on it that I read years ago, so I wouldn't have to rely on pure memory here...Please do not ask for code as refusal often offends.
- 05-08-2012, 06:53 PM #3
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
- 05-08-2012, 07:09 PM #4
Re: anon inner class
The reason could be because the variable is defined locally to method/constructor. The compiler wants a way to keep its value past the exiting of the method.
If you don't understand my response, don't ignore it, ask a question.
- 05-08-2012, 08:17 PM #5
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
- 05-08-2012, 08:21 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: anon inner class
Local variables are gone when the enclosing method terminates but that class object survices and so everything that is used by that object still has to be there; marking local variables final ensures that they survice the termination of the method.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-08-2012, 10:21 PM #7
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
- 05-08-2012, 11:39 PM #8
Re: anon inner class
No, you could make them member (aka instance) variables by simply changing where you declare them:
Java Code:public class MyApp2{ JFrame frame; JTextField tf; JButton button; public MyApp2(){ frame = new JFrame ("My App"); tf = new JTextField(); button = new JButton("ok"); button.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ System.out.println(tf.getText()); } } ); } }Get in the habit of using standard Java naming conventions!
- 05-09-2012, 02:46 AM #9
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
Re: anon inner class
But why did my intial one require the final declaration on it?
- 05-09-2012, 02:49 AM #10
Re: anon inner class
The variable's value would be gone when the method exited. Did you miss the explanations given earlier?
If you don't understand my response, don't ignore it, ask a question.
- 05-09-2012, 02:58 AM #11
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
Re: anon inner class
But if the new method cant see the variable tf, how does declaring as final make a difference?
- 05-09-2012, 03:07 AM #12
Re: anon inner class
See the previous posts.
If you don't understand my response, don't ignore it, ask a question.
- 05-09-2012, 03:20 AM #13
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
Re: anon inner class
So if we declare it as final it kind of becomes like an instance variable?
Sorry for being so slow but I'm just finding it confusing as there are so many different ways of doing things in java for the most simple cases.
Thanks for your patience
- 05-09-2012, 03:24 AM #14
Re: anon inner class
final makes its value available to the method in the anonymous class.
If you don't understand my response, don't ignore it, ask a question.
- 05-09-2012, 04:58 AM #15
Re: anon inner class
Get in the habit of using standard Java naming conventions!
Similar Threads
-
How to get a compatible class of a template class? Return type of method is AClass<E>
By SKuypers in forum Advanced JavaReplies: 0Last Post: 12-07-2011, 11:55 AM -
Eclipse Compile Error: Call validateValue(Class<T>, String, Object, Class<?>...)
By Tomshi in forum EclipseReplies: 0Last Post: 03-27-2011, 05:49 AM -
super class reference variable accesses overriding sub class method
By subith86 in forum New To JavaReplies: 5Last Post: 01-26-2011, 06:38 PM -
Dynamic loading of a class (passing class definition over the network)
By eddie-w in forum Advanced JavaReplies: 8Last Post: 04-14-2010, 05:49 AM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks