Results 1 to 15 of 15
  1. #1
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default 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

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default 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:
    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);
    It's possible that the action listener could be referring to the wrong text field.
    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.

    ** This space for rent **

  3. #3
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: anon inner class

    Quote Originally Posted by Tolls View Post
    This ensures the reference tf does not change, and so is not accidentally reflected in the inner class.
    Something along the lines of:
    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);
    It's possible that the action listener could be referring to the wrong text field.
    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...
    Thanks!!!

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

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

  5. #5
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: anon inner class

    Quote Originally Posted by Norm View Post
    The compiler wants a way to keep its value past the exiting of the method.
    What do you mean by this?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

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

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: anon inner class

    Quote Originally Posted by JosAH View Post
    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,

    Jos
    Does this mean we always have to declare them as final???

  8. #8
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

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

  9. #9
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: anon inner class

    But why did my intial one require the final declaration on it?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

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

  11. #11
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: anon inner class

    But if the new method cant see the variable tf, how does declaring as final make a difference?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: anon inner class

    See the previous posts.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default 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

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

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

  15. #15
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2011, 11:55 AM
  2. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  3. Replies: 5
    Last Post: 01-26-2011, 06:38 PM
  4. Replies: 8
    Last Post: 04-14-2010, 05:49 AM
  5. Replies: 8
    Last Post: 03-21-2009, 09:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •