Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Unused Variable in ActionEvent()

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Unused Variable in ActionEvent()

    Greetings All

    I am a C++ programmer and currently learning Java.

    While playing around with event management I always find that I have an unused variable in the ActionEvent(). For example:

     
    import javax.swing.JFrame;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
     
     
    public class ExampleProgram extends JFrame {
     
        ExampleProgram(){
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            initGui();
     
            return;
        }
     
        public static void main(String args[]){
     
            ExampleProgram w = new ExampleProgram();
            w.setVisible(true);
     
            return;
     
        }
     
        private void initGui(){
     
            JButton b = new JButton();
            b.setText("Click Me!");
     
            b.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent evt){
                    doStuff(evt);
                }
            });
     
            this.add(b);
            this.setSize(300,200);
     
            return;
        }
     
        private void doStuff(ActionEvent evt){
            System.out.println("Yeah!");
     
            return;
        }
    }
     
     
    }

    Variable evt is always unused. All my coding experience tells me that unused variables are a horrible thing to see and so should always be removed. At least this is what I do in C++.

    Am I missing something, doing something fundamentally wrong or just being silly?

    I welcome any and all advice.

    Regards
    Nikki


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Unused Variable in ActionEvent()

    So, go ahead and remove it, and see what happens.

    Usually, unused variables should be removed (if only for your own sanity). However, sometimes you can't just remove a parameter- for example, you can't get rid of the ActionEvent passed into your actionPerformed() method.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Unused Variable in ActionEvent()

    Did you read the API doc for ActionEvent? It holds information about the recently occurred event your code is handling. It's practically worthless if you're using the anonymous innner class pattern to handle button clicks - as you are - because you're almost guaranteed (unless the API docs explicitly guarantees!) that no other event will cause your handler method to fire.

    One very common other way of handling AWT events in noddy applications is to implement ActionListener in whatever class of your own is the top-level Frame for the application. That way all ActionEvents are handled by a single actionPerformed in your extended Frame class, which must then examine ActionEvent.getSource() to discover which button / control etc is responsible for firing the event.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Unused Variable in ActionEvent()

    The API for ActionEvent won't really help the OP, as the question was more about unused variables than the actual value of the variable.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Sep 2011
    Location
    United States
    Posts
    30
    My Mood
    Fine
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Unused Variable in ActionEvent()

    If you only care that the event happened and not what caused the event then there is no point in passing evt in the method doStuff(). In your example program evt will just be sitting around being unused, that's just how things are. However, when you go to make the program more complex you may need to use evt to get data about the event; such as ewt.getSource() as Sean4u stated.

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Unused Variable in ActionEvent()

    Just a quick message to say thanks to everyone that responded.

    I think I understand a little more now

    Regards
    Nikki

Similar Threads

  1. [SOLVED] what is this? Variable?
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 8th, 2011, 12:32 AM
  2. [SOLVED] Problem with ActionEvent not doing anything
    By thisisnic in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 14th, 2011, 02:13 PM
  3. How to use the same variable in various JPanel?
    By danielpereira in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 19th, 2010, 01:08 PM
  4. How to generate an ActionEvent progrematically
    By nasi in forum Java Theory & Questions
    Replies: 7
    Last Post: May 22nd, 2010, 01:31 PM
  5. How to do thread communication in java
    By Koren3 in forum Threads
    Replies: 4
    Last Post: March 29th, 2009, 11:49 AM