Vedant Terkar
Vedant Terkar

Reputation: 4682

Why is the semicolon not required but allowed at the end of a class definition?

I'm trying to shift from C++ to Java.

What I wonder is, in C++, after a class definition, a semicolon (;) is required, but in Java it isn't.

That is, in C++:

class Person {
    public:
    string name;
    int number;
}; // Note this semicolon

But in Java:

class Person {
    public String name;
    public int number;
} // Semicolon is not required

That's fine, I understand that.

However, my problem is:

Java also works when I add semicolon at end of class definition, like:

class Person {
    public String name;
    public int number;
}; // Now this semicolon is confusing me!

I've compiled and executed both the program snippets shown for Java, and they both work. Can anyone explain why this is so? What does the semicolon at the end of a class definition in Java stand for?

I'm sorry if this question is of low quality, but I really need clarification for this. I hope experts in Java will help me.

Well, I've already seen Semicolons in a class definition and other related questions.

Thanks in advance.

Upvotes: 20

Views: 26270

Answers (5)

Stephen C
Stephen C

Reputation: 719259

I've compiled and executed both the program snippets shown for Java, and they both work. Can anyone explain why this is so?

It is allowed because the Java Grammar says it is allowed; See JLS 7.6.

What does the semicolon at the end of a class definition in Java stand for?

Nothing. It is optional "syntactic noise".

The JLS explains it as follows:

Extra ";" tokens appearing at the level of type declarations in a compilation unit have no effect on the meaning of the compilation unit. Stray semicolons are permitted in the Java programming language solely as a concession to C++ programmers who are used to placing ";" after a class declaration. They should not be used in new Java code.


(Note that this is NOT an "empty statement". An empty statement (JLS 14.6) appears in a syntactic context where a statement is allowed. The presence of an empty statement can change the meaning of your code; e.g. if (a == b) ; c(); versus if (a == b) c();)

Upvotes: 30

Pythoner
Pythoner

Reputation: 5595

I think this is a followed style which inherites from C:

struct A
{ 
    unsigned a;
    unsigned b;
};

Or you can use:

struct A
{ 
    unsigned a;
    unsigned b;
}A1,A2;

Upvotes: 0

Andy Thomas
Andy Thomas

Reputation: 86459

In the early days, allowing this was one of the ways in which Java was made more familiar to C/C++ programmers. This familiarity was important to the adoption of Java.

Here's how it works syntactically.

After a top-level class, it works because a compilation unit contains this sequence, according to JLS 7.3:

CompilationUnit:
 PackageDeclaration(opt) ImportDeclarations(opt) TypeDeclarations(opt)

And a type declaration is any one of the following, according to JLS 7.6:

TypeDeclaration:
   ClassDeclaration
   InterfaceDeclaration
   ;

After a member class, nested in some other class, it works because a semicolon can be a class member declaration, according to JLS 8.1.6:

ClassMemberDeclaration:
    FieldDeclaration
    MethodDeclaration
    ClassDeclaration    
    InterfaceDeclaration
    ;

After a local class, inside a method, it works because a semicolon can be an empty statement, according to JLS 14.6:

EmptyStatement:
    ;

Upvotes: 11

rwols
rwols

Reputation: 3078

I'm not familiar with Java, but the reason why there's an extra semicolon needed is because you can define an anonymous class, inside functions. For instance:

void routine(const int x, const int y)
{
    class { public: int x; int y; } my_instance;
    my_instance.x = x;
    my_instance.y = y;
    // ... etc
}

You'll usually see this more with structs than with classes, to capture some important variables of a big class.

void f(const BigClass& big_class)
{
    struct { std::string str; int i; } props;
    props.str = big_class.GetFilename();
    props.i = big_class.GetID();
    // etc...
}

Upvotes: -2

gexicide
gexicide

Reputation: 40098

Java added this just for persons like you who switch from C++!

In Java, a single semicolon is a declaration that may be written almost everywhere. Its only purpose is to ease the transition from such languages.

For example, also the following is correct in Java:

;;;;;;;;;;
class X{
    ;;;;;;;;;;;;;;
} ;;;;;;;;;;;;;;;

A semicolon is simply treated as an empty declaration that does nothing.

Here is a quote from the spec, paragraph 7.6:

Extra ";" tokens appearing at the level of type declarations in a compilation unit have no effect on the meaning of the compilation unit. Stray semicolons are permitted in the Java programming language solely as a concession to C++ programmers who are used to placing ";" after a class declaration. They should not be used in new Java code.

So as you see, this is really just for guys like you :).

You can even use a line of semicolons as a nice visual separation. However, I strongly advise against this. But it might be good for bragging purposes. E.g.:

class X {
   // Member variables
   private int i;

   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   // Constructors
   X(){}

   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   // Methods
   void foo(){}    

}

Upvotes: 10

Related Questions