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

Vedant Terkar

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.

Stephen C

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();)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is class of T not allowed in a generic type?

From Dev

Why is no empty argument list required inside this class definition?

From Dev

Why can't dill/pickle class definition?

From Dev

Why Object Class definition makes difference

From Dev

Why is `forall` required to have a polymorphic type in data definition?

From Dev

Is a class declaration allowed after a class definition?

From Dev

Declaration and declaration with definition. Why is this not allowed?

From Dev

Why is "where T : class" required in this example?

From Dev

friend not allowed outside of a class definition

From Dev

Why super keyword in generics is not allowed at class level

From Dev

Why is a static local class not allowed in a method?

From Dev

Friend function in-class definition only allowed in non-local class definitions. What does it mean?

From Dev

C++ - Scope of the object created at the end of class definition

From Dev

Why must I include a semicolon when putting a label at the end of a function?

From Dev

Why no semicolon after Ok(()) at the end of a function in rust?

From Dev

Why is usage of instanceof is not allowed, but Class.isInstance() is allowed in Generics?

From Dev

Why is an extra semicolon not allowed after the return statement, when it is allowed for other statements?

From Dev

Why is a static local class not allowed in a method?

From Dev

Why super keyword in generics is not allowed at class level

From Dev

Why must I include a semicolon when putting a label at the end of a function?

From Dev

Why class required default constructor but structure dont required?

From Dev

Why is class of T not allowed in a generic type?

From Dev

Why is interface inheritance allowed on a struct and why can a class not be inherited

From Dev

Why can't dill/pickle class definition?

From Dev

Why don't we use semicolon at the end of class in java?

From Dev

Why Object Class definition makes difference

From Dev

Missing semicolon(;) at end of SQL statement

From Dev

Friend function in-class definition only allowed in non-local class definitions. What does it mean?

From Dev

C++ - Scope of the object created at the end of class definition

Related Related

  1. 1

    Why is class of T not allowed in a generic type?

  2. 2

    Why is no empty argument list required inside this class definition?

  3. 3

    Why can't dill/pickle class definition?

  4. 4

    Why Object Class definition makes difference

  5. 5

    Why is `forall` required to have a polymorphic type in data definition?

  6. 6

    Is a class declaration allowed after a class definition?

  7. 7

    Declaration and declaration with definition. Why is this not allowed?

  8. 8

    Why is "where T : class" required in this example?

  9. 9

    friend not allowed outside of a class definition

  10. 10

    Why super keyword in generics is not allowed at class level

  11. 11

    Why is a static local class not allowed in a method?

  12. 12

    Friend function in-class definition only allowed in non-local class definitions. What does it mean?

  13. 13

    C++ - Scope of the object created at the end of class definition

  14. 14

    Why must I include a semicolon when putting a label at the end of a function?

  15. 15

    Why no semicolon after Ok(()) at the end of a function in rust?

  16. 16

    Why is usage of instanceof is not allowed, but Class.isInstance() is allowed in Generics?

  17. 17

    Why is an extra semicolon not allowed after the return statement, when it is allowed for other statements?

  18. 18

    Why is a static local class not allowed in a method?

  19. 19

    Why super keyword in generics is not allowed at class level

  20. 20

    Why must I include a semicolon when putting a label at the end of a function?

  21. 21

    Why class required default constructor but structure dont required?

  22. 22

    Why is class of T not allowed in a generic type?

  23. 23

    Why is interface inheritance allowed on a struct and why can a class not be inherited

  24. 24

    Why can't dill/pickle class definition?

  25. 25

    Why don't we use semicolon at the end of class in java?

  26. 26

    Why Object Class definition makes difference

  27. 27

    Missing semicolon(;) at end of SQL statement

  28. 28

    Friend function in-class definition only allowed in non-local class definitions. What does it mean?

  29. 29

    C++ - Scope of the object created at the end of class definition

HotTag

Archive