Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Why the semicolon after the curly braces

    Hello. Why is it necessary to have a semicolon after a closing curly brace? I thought the purpose of the curly braces was simply to identify a code block. So what purpose does the semicolon after a closing curly braces serve?

    Ex. The compiler is telling me I need a semicolon after the last closing curly brace of this block of code. With the added semicolon, the code compiles OK.
    Java Code:
    ActionListener action = new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {
    				if (boat != null) {
    					boat.updateForNewFrame();
    					bomb.updateForNewFrame();
    					sub.updateForNewFrame();
    				}
    				repaint();
    			}
    		}
    Thanks in advance.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why the semicolon after the curly braces

    It's similar to int x= 42; it also needs a semicolon at the end. The new ActionListener() { ... } part serves the same purpose as '42' in my example, i.e. it's the value part.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: Why the semicolon after the curly braces

    I understand the semicolon at the end of the int x = 42; line.

    I don't understand the semicolon after a closing curly braces because in between the assignment because there is this large block of code with opening and closing curly braces.

    Here is another (I think related) misunderstanding I am having.
    Java Code:
    addMouseListener(new MouseAdapter()) {
    			// The MouseListener request focus when the user clicks on the panel.
    			public void mousePressed(MouseEvent evt) {
    				requestFocus();
    			}
    		}
    In the first line I have written "(new MouseAdapter()) {" and this is apparently incorrect. The closing parens in that line is supposed to go after the curly brace that ends the code block, with a semicolon after. Like below:

    Java Code:
    addMouseListener(new MouseAdapter() {
    			// The MouseListener request focus when the user clicks on the panel.
    			public void mousePressed(MouseEvent evt) {
    				requestFocus();
    			}
    		} );
    Which kinda blows away what I though I knew about semicolons and curly braces. As usual, the problem lies between my ears. thank goodness I got this example out of a text. I don't think I would ever find the fix to the error on my own.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why the semicolon after the curly braces

    The construct new ClassOrInterface() { ... } defines an anonymous extension or implementation of ClassOrInterface. In your example, you have:

    Java Code:
    addMouseListener(<xxx>);
    where <xxx> is:

    Java Code:
    new MouseAdapter() { ... }
    which is an anonymous extension of the MousAdapter class; in full <xxx> reads:

    Java Code:
    new MouseAdapter() {
                public void mousePressed(MouseEvent evt) {
                    requestFocus();
                }
    }
    If you stick the above in an addMouseListener(<xxx>) you get:

    Java Code:
    addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent evt) {
                    requestFocus();
                }
    });
    and that's where the funky placement of the curly brackets, parentheses and semicolons comes from.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: Why the semicolon after the curly braces

    Thanks for your help. I am slowly understanding.

Similar Threads

  1. Replies: 6
    Last Post: 05-21-2012, 07:33 PM
  2. Double semicolon, why no error?
    By africanhacker in forum New To Java
    Replies: 2
    Last Post: 03-13-2011, 01:10 PM
  3. Right-side for left curly braces
    By zweibieren in forum AWT / Swing
    Replies: 5
    Last Post: 11-02-2009, 09:08 AM
  4. Replies: 5
    Last Post: 01-11-2009, 11:12 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
  •