1

Is there any difference in between the two method definitions below? Eclipse does not complain for any of them.

private void method1() {
}

and

private void method2() {    
};
Devjosh
  • 6,450
  • 4
  • 39
  • 61
user1016403
  • 12,151
  • 35
  • 108
  • 137
  • 2
    see here: http://stackoverflow.com/questions/2724371/when-would-you-put-a-semicolon-after-a-method-closing-brace Semicolon is allowed by the grammar, but is not necesarry here and isn't used by most people in such cases. – jham Feb 01 '12 at 11:22

4 Answers4

6

The ; does not help and hurt. It does not belong to method2() and will be ignored by the compiler.

Devjosh
  • 6,450
  • 4
  • 39
  • 61
4

The semicolon is not part of the method so there is no difference between the method definitions.

The semicolon is just part of the class body.

class AClass {
    private void method() { }

    ;
}

This is equivalent.

You can put initializer statements in the class body. They are executed when an instance is created. A single semicolon constitutes an empty statement so it is not very useful. Other initializers are more useful:

class AClass {
    private void method() { }

    ;

    int x = 5;

    {
        System.out.println("Hello world");
    }

    int y = 5; ; ;
}
Hauke Ingmar Schmidt
  • 11,559
  • 1
  • 42
  • 50
  • What kind of code formating is this? –  Feb 01 '12 at 11:26
  • 1
    Standard? Well... most of the time. The empty statements are useless so I didn't put them on a new line just to demonstrate that they have no effect. But except for that: http://www.oracle.com/technetwork/java/codeconventions-141270.html#18761 – Hauke Ingmar Schmidt Feb 01 '12 at 11:32
  • You are correct. I looks so very odd :) –  Feb 01 '12 at 11:33
1

There should not be any semicolon after the closing braces of the method.

But if you do put it, the compiler will just consider it as an empty statement and hence it is not giving your any issues.

bluefalcon
  • 4,225
  • 1
  • 32
  • 41
1

Syntactic sugar.

(you can always run javap to make sure the bytecodes are the same)

Marcelo
  • 4,580
  • 7
  • 29
  • 46