0

Is it syntactically correct to have java classed ending with ; In most of the documentation java class syntax does not end with ;

However i successfully able to compile below code,

public class ClassDemo {
    public static void main (String args[]) {
        System.out.println ("Hello World!");
    }
};

My question is, is it correct to end a class declaration with a ;? Why is it ignored by the compiler, and seems to be indifferent whether there is a ; at the end or not?

5
  • 2
    What is your question? Commented Mar 16, 2015 at 11:55
  • 2
    This provision may have been provided by the Java language compiler because of anonymous classes which must need ; Commented Mar 16, 2015 at 11:57
  • simple logic if you get any compilation error then its wrong ,if you dont get error then you right. So However i successfully able to compile below code, means you are right Commented Mar 16, 2015 at 11:58
  • it will be useful en.wikipedia.org/wiki/… Commented Mar 16, 2015 at 12:07
  • Helpful link: stackoverflow.com/questions/24614393/… Commented Mar 16, 2015 at 13:10

6 Answers 6

4

Your code is correct by syntax but ; is unnecessary.

From JLS 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.

Sign up to request clarification or add additional context in comments.

2 Comments

isnt this best suited in comment?
@SpringLearner Initially yes. But I was updating my answer
0

It is syntactically valid but not necessary, and omitted by convention.

Comments

0

Its just a terminator so if you include anywhere and any number of times then it doesn't effect your program.

Comments

0

";" tokens appearing at the level of type declarations in a compilation unit have no effect on the meaning of the compilation unit. So its optional one.

Comments

0

I think you are coming from background like javascript. In javascript it is good practice to end your class/method/scripts with ;. Because if you do not do so, it causes problem for another javacript loaded after that as it is assumed continued part of earlier script.

However in java class defination is ended by }.Anything after this will not be part of this class and is not issue for compiler.

In fact, you can have as many; in any line of java. It does not make any difference to java compiler.

Comments

0

Java Class end with Semicolon(;)

In computer programming, the semicolon is often used to separate multiple statements (for example, in Perl, Pascal, PL/I, and SQL; see Pascal: Semicolons as statement separators). In other languages, semicolons are called terminators and are required after every statement (such as in Java, and the C family). Today semicolons as terminators (or no semicolons) has largely won out, but this was a divisive issue in programming languages from the 1960s into the 1980s.An influential and frequently-cited study in this debate was Gannon & Horning (1975), which concluded strongly in favor of semicolon as a terminator:

The most important was that having a semicolon as a statement terminator was better than having a semicolon as a statement separator. So that JAVA consider class end with (;) as class terminator. if you put other punctuation like ., or letters compiler will through an error

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.