java - Syntax Error: Import statement with multiple semi-colon -


this might weird question, valid one. know below statement not have compilation error:

arraylist list = new arraylist();;; //(with 3 `;` semi-colon) 

okay have written below import statement:

import java.util.arraylist;;; (with 3 `;` semi-colon) 

but got below compilation error:

syntax error on token ";", invalid staticimportondemanddeclarationname 

why?

what have here:

arraylist list = new arraylist();;; //(with 3 `;` semi-colon) 

is not statement terminated 3 semi-colons. it's statement terminated 1 semi-colon, followed 2 empty statements.

an empty statement legal in java, import section of java source file not comprised of statements, it's comprised of import declarations.

jls 14.6 defines empty statement:

an empty statement nothing.

  emptystatement:      ; 

execution of empty statement completes normally.

a perhaps-legitimate use of empty statement:

//loop forever while (true) {;} // body of loop empty statement. 

in other words, in first example, have assignment followed 2 empty statements. semicolons not strictly superfluous.

the import section has own grammar, , none of grammar rules allow arbitrary semi-colon. grammar specified jls 7.5:

importdeclaration:     singletypeimportdeclaration     typeimportondemanddeclaration        singlestaticimportdeclaration        staticimportondemanddeclaration  singletypeimportdeclaration:     import typename ;  ... 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -