Classes ending with semi-colon

R

Razvan

Hi,




I saw today some class definitions ending with semi-colon (C++ style)

public class CDummy
{
public static void main(String args[])
{
System.out.println("CDummy");
}

};

Please notice the semicolon at the end. Is this the standard
behaviour or it is just my compiler that allows this ?



Regards,
Razvan
 
P

Paul Lutus

Razvan said:
Hi,




I saw today some class definitions ending with semi-colon (C++ style)

public class CDummy
{
public static void main(String args[])
{
System.out.println("CDummy");
}

};

Please notice the semicolon at the end. Is this the standard
behaviour or it is just my compiler that allows this ?

It's technically wrong, but it doesn't raise a compiler error message. This
is not the normal practice in Java.

Be careful, though. When you declare an array of constants, the semicolon is
required:

String data[] = {
"this",
"is",
"a",
"test"
};
 
L

Liz

Paul Lutus said:
Razvan said:
Hi,




I saw today some class definitions ending with semi-colon (C++ style)

public class CDummy
{
public static void main(String args[])
{
System.out.println("CDummy");
}

};

Please notice the semicolon at the end. Is this the standard
behaviour or it is just my compiler that allows this ?

It's technically wrong, but it doesn't raise a compiler error message. This
is not the normal practice in Java.

Be careful, though. When you declare an array of constants, the semicolon is
required:

String data[] = {
"this",
"is",
"a",
"test"
};

I think you can stick an extra comma after the last element
in the list - > "test",
 
P

Paul Lutus

Liz wrote:

/ ...
Be careful, though. When you declare an array of constants, the semicolon is
required:

String data[] = {
"this",
"is",
"a",
"test"
};

I think you can stick an extra comma after the last element
in the list - > "test",

Yes, but this is just a way to avoid a gratuitous error, it is not good
form. It's the same idea as the semicolon at the end of the class in the
original example.
 
G

Guest

Razvan said:
I saw today some class definitions ending with semi-colon (C++ style)

public class CDummy
{
public static void main(String args[])
{
System.out.println("CDummy");
}

};

Please notice the semicolon at the end. Is this the standard
behaviour or it is just my compiler that allows this ?

It's legal.
The Java grammar says:

CompilationUnit:
[package QualifiedIdentifier ; ] {ImportDeclaration}
{TypeDeclaration}

TypeDeclaration:
ClassOrInterfaceDeclaration
;

So the semicolon after your CDummy class is just an empty declaration.

- Dario
 
A

Alan Moore

It's technically wrong, but it doesn't raise a compiler error message. This
is not the normal practice in Java.

It can cause some third-party tools to barf, though, so don't do it.
 
P

P.Hill

String data[] = {
Paul said:
Yes, but this is just a way to avoid a gratuitous error, it is not good
form. It's the same idea as the semicolon at the end of the class in the
original example.

The array one is defined in the language spec, doesn't seem to appear
in the spec:

Class definition:
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#18988
Array Initializer:
http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html#11358

My definition for good is that it matches the specified syntax, what definition
of good are you using?

I say Liz's example is perfectly good, but looking at Syntax in Chapter 18 of
the JLS, the semi-after it turns out also to be good.

The whole thing starts with:
CompilationUnit:
[package QualifiedIdentifier ; ] {ImportDeclaration}
{TypeDeclaration}

A Type declartion is defined:

TypeDeclaration:
ClassOrInterfaceDeclaration
;

Which seems to mean that surprisingly enough a final semicolumn is
a degenerate type definitation, so actually IS legal Java.

Weird, really weird,
-Paul H.
 
L

Liz

P.Hill said:
String data[] = {
"this",
"is",
"a",
"test"
};
Liz said:
I think you can stick an extra comma after the last element
in the list - > "test",
Paul said:
Yes, but this is just a way to avoid a gratuitous error, it is not good
form. It's the same idea as the semicolon at the end of the class in the
original example.

The array one is defined in the language spec, doesn't seem to appear
in the spec:

Class definition:
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#1898
8
Array Initializer:
http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html#11358

My definition for good is that it matches the specified syntax, what definition
of good are you using?

I say Liz's example is perfectly good, but looking at Syntax in Chapter 18 of
the JLS, the semi-after it turns out also to be good.

The whole thing starts with:
CompilationUnit:
[package QualifiedIdentifier ; ] {ImportDeclaration}
{TypeDeclaration}

A Type declartion is defined:

TypeDeclaration:
ClassOrInterfaceDeclaration
;

Which seems to mean that surprisingly enough a final semicolumn is
a degenerate type definitation, so actually IS legal Java.

Weird, really weird,

Not weird, the rationalization is that you can add additional
array elements at the end without having to change this line.
Only important when using a versioning system.
 
R

Razvan

http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#18988


Copy & paste from the above link:

ClassBody:
{ ClassBodyDeclarations }

So, the class body has no semi-colon after it.
Inside a class body you can define variables, member functions,
classes (nested classes) or interfaces. Look how the
"ClassBodyDeclarations" is declared:


ClassBodyDeclarations:
ClassBodyDeclaration
ClassBodyDeclarations ClassBodyDeclaration

ClassBodyDeclaration:
ClassMemberDeclaration
InstanceInitializer
StaticInitializer
ConstructorDeclaration

ClassMemberDeclaration:
FieldDeclaration
MethodDeclaration
ClassDeclaration
InterfaceDeclaration
;


That means that when defining "ClassMemberDeclaration" you can
end with semi-colon, but it does not say that you can do the same for
the enclosing class.




Regards,
Razvan
 
R

Razvan

It can cause some third-party tools to barf, though, so don't do it.

Please name one that fails.



Regards,
Razvan
 
C

Chris Uppal

P.Hill said:
The array one is defined in the language spec, doesn't seem to appear
in the spec:
[...]
Array Initializer:
http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html#11358

My definition for good is that it matches the specified syntax, what
definition of good are you using?

I believe that array initialisers are one of the places where the language
defined in the body of the JLS2 differs from the language defined in the final
chapter. The body text does not allow the final ',' but chapter 18 does.
Grr...

I don't know which is intended to be normative, though the following comment,
from the beginning of Ch18, might be taken as some sort of hint -- by the
sufficiently credulous ;-)

=====
The grammar presented piecemeal in the preceding chapters is much better for
exposition, but it is not ideally suited as a basis for a parser. The grammar
presented in this chapter is the basis for the reference implementation.
=====

BTW, my take on the way that C and (maybe) Java allow the final ',' is that it
is good (i.e. recommended -- by me) for multiline initialisers:

... {
'aaa',
'bbb'
// etc
yyy,
}

since lines may be added and removed without treating the last one as a
special-case. It also (and this is an important point, IMO) simplifies code
generation usefully.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top