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

Thread: Concern about use of });

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Concern about use of });

    I am looking at some programs online and see that they close with

    }
    });
    }

    It looks strange to me to have

    });

    and not just

    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Concern about use of });

    Please provide an example. I suspect there's more going on there than you've described; more than a program closing but likely a closing to a statement that included the creation of an anonymous inner class or a similar construction.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Concern about use of });

    This is what was written as copied from the tutorial. I am just concerned having to learn this on my own. I an absorbing things bit by bit

    thanks for your help
    import javax.swing.*;
     
    public class FrameExample_Extended{
     
        private static void createAndShowGUI() {
     
            //This is to turn on the Default 'Look and Feel'.
            JFrame.setDefaultLookAndFeelDecorated(true);
     
            JFrame frame = new JFrame("[=] Hello World [=]");
     
            // To make sure the default operation on pressing the close button on the window-bar
            // is to exit the program.
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            // To size the window to fit all the widgets in, without working out the size manually.
            frame.pack();
            frame.setVisible(true);
        }
     
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    Last edited by helloworld922; September 7th, 2013 at 11:34 AM. Reason: please use [code] tags

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Concern about use of });

    That is the end of a statement. Look at the SwingUtilities.invokeLater(...) method in the code.

    new Runnable() {
    public void run() {
    createAndShowGUI();
    }
    }
    Is the parameter for the SwingUtilities.invokeLater(...) method. That ); is the closing parentheses for the invokeLater method.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Concern about use of });

    Thanks

    How important is this SwingUtilities.invokeLater(...) method

    Is it used often in programs or can I get by without ever using it

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Concern about use of });

    Thanks for posting the second bit in code or highlight tags.

    The first example you posted,
        public static void main(String[] args)
        {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            SwingUtilities.invokeLater( new Runnable()
            {
                public void run()
                {
                    createAndShowGUI();
                }
            } );
          }
    is constructing an anonymous inner class, Runnable, which requires a method run(). If you trace the construction carefully, you'll see the invokeLater() method takes a Runnable argument and the Runnable class is constructed between the open and close parentheses of invokeLater. The semi-colon is necessary at the end to end the statement that starts with SwingUtilities. . . .

    Using SwingUtilities.invokeLater() runs or starts the Swing application on the Event Dispatch Thread, or EDT, and yes, it's important. There is more than one way to accomplish the same thing, but learn this way, memorize it, and use it. There are many Swing tutorials on the web that are bad examples because they don't launch their apps this way. It's wrong, it's bad, and now you know better. You can learn more about why it's important by searching and reading up on "Concurrency in Swing," and "Swing on the EDT," and similar searches.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Concern about use of });

    Thanks

    The whole thing seems a bit advanced to me but when I go into it further I will remember this advice

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Concern about use of });

    The ; just ends a statement. For examples:
    frame.pack();
    frame.setVisible(true);

    frame.pack() has no parameters.
    frame.setVisible(true) takes one parameter, a boolean, in this example, true.


    SwingUtilities.invokeLater takes one parameter, a Runnable object. Rather than passing such an object in, there is one being created on the spot. This is where all of the {} come in to play.
    Consider this:
    SwingUtilities.invokeLater(MyRunnable);//This assumes MyRunnable implements Runnable.
    Now this:
    SwingUtilities.invokeLater(new Runnable() {
       });
    See how the ); still ends the statement as in the above examples. The problem here is that the Runnable interface requires the implementation of a method with the signature: public void run()

    Which leads to this:
    SwingUtilities.invokeLater(new Runnable() {
       public void run() {
          //the statements to be executed
       }
    });
    So in the end it is really not a }); but the } closes off the implementation of the new Runnable, the ) closes off the end of the method invokeLater and the ; closes off the statement.

  9. #9
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Concern about use of });

    Thanks again for this comprehensive reply I will study it as soon as I get a chance

    However how does MyRunnable implements Runnable. is it like a function within a function

    As soon as I think it through I will probably ask the question in a different way

  10. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Concern about use of });

    MyRunnable is just a theoretical class that could exist. Created to show how invokeLater(); is just another method call, just another statement, and has to end with a ;
    The idea to grab is that the parameter being passed to the invokeLater method is an anonymous class defined inside the parentheses of the method, resulting in the odd looking series of });
    Some reading on the subject

  11. #11
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: Concern about use of });

    }
    });
    }

    It looks strange to me to have

    });

    and not just

    }
    The others provided better answers, but here's the short version... Any time you see {, (, or [, there must be an accompanying }, ), or ] and vice-versa. Each of the first three opens some sort of element, and the last three close that element. If you open something but don't close it, or try to close something that isn't opened, the program just doesn't compile at all, full stop. And the closings have to come in the reverse order of the openings. So if you see {(, you need to close it with )}.

    It's kind of like those Russian nesting dolls. If you're putting away the whole set, you need to close each and every one of them. You can't put the small and large dolls away, then forget about the medium one. Further, you can't put away the small one, then the large one, then the medium one. You have to pack them away the same way you unpacked them.