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() {
};
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() {
};
The ;
does not help and hurt. It does not belong to method2()
and will be ignored by the compiler.
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; ; ;
}
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.
Syntactic sugar.
(you can always run javap
to make sure the bytecodes are the same)