When would you put a semicolon after a method closing brace?

前端 未结 4 582
轮回少年
轮回少年 2020-12-02 01:43

I\'ve been programming in Java for a while, and I\'ve just come across this syntax for the first time:

public Object getSomething(){return something;}; 


        
相关标签:
4条回答
  • 2020-12-02 02:04

    It's allowed by the grammar as a concession to harmless syntax errors, but it's not generally used and doesn't mean anything different (than leaving the semicolon out).

    Just as a }; inside a method (such as after an if block) is a null statement and is allowed, an errant semicolon outside a method is considered a null declaration and is allowed.

    Specifically, the following production from the Java Language Specification allows this:

    ClassBodyDeclaration:
      ; 
      [static] Block
      ModifiersOpt MemberDecl
    
    0 讨论(0)
  • 2020-12-02 02:23

    It's simply an empty statement - it is most likely a typo.

    Consider the fact that in all C-based languages, a statement is terminated with a semicolon. A hanging semicolon like this simply terminates the current statement which in this case is nothing.

    0 讨论(0)
  • 2020-12-02 02:24

    A semicolon is interpreted as an empty statement, which is permissible whereever a statement is permissible.

    EmptyStatement:
     ;
    

    As Java specification says

    http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.6

    0 讨论(0)
  • 2020-12-02 02:26

    Or is it just something that is allowed but generally not used?

    Yeap, that's it. It is valid Java but doesn't do anything:

    public class X  {
        ;;;;;;;;;;;;;;;;;;
        ;;;;;
        ;
    }
    
    0 讨论(0)
提交回复
热议问题