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 don't we use semicolon at the end of class in java?

From Dev

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

From Dev

friend not allowed outside of a class definition

From Dev

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

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 an extra semicolon not allowed after the return statement, when it is allowed for other statements?

From Dev

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

From Dev

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

From Dev

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

From Dev

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

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 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

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

From Dev

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

From Dev

Why Object Class definition makes difference

From Dev

Why can't dill/pickle class definition?

From Dev

Why can't dill/pickle class definition?

From Dev

Why Object Class definition makes difference

From Dev

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

From Dev

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

From Dev

Missing semicolon(;) at end of SQL statement

From Dev

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

From Dev

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

From Dev

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

From Dev

Why class required default constructor but structure dont required?

From Dev

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

Related Related

  1. 1

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

  2. 2

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

  3. 3

    friend not allowed outside of a class definition

  4. 4

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

  5. 5

    Is a class declaration allowed after a class definition?

  6. 6

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

  7. 7

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

  8. 8

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

  9. 9

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

  10. 10

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

  11. 11

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

  12. 12

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

  13. 13

    Why super keyword in generics is not allowed at class level

  14. 14

    Why super keyword in generics is not allowed at class level

  15. 15

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

  16. 16

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

  17. 17

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

  18. 18

    Why Object Class definition makes difference

  19. 19

    Why can't dill/pickle class definition?

  20. 20

    Why can't dill/pickle class definition?

  21. 21

    Why Object Class definition makes difference

  22. 22

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

  23. 23

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

  24. 24

    Missing semicolon(;) at end of SQL statement

  25. 25

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

  26. 26

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

  27. 27

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

  28. 28

    Why class required default constructor but structure dont required?

  29. 29

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

HotTag

Archive