有什么理由应该将java中的package语句放在开头? [英] Is there any reason that the package statement in java should be at the beginning?

查看:745
本文介绍了有什么理由应该将java中的package语句放在开头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Java编程语言开始;我有一个简单的问题:

I am beginning with the Java Programming language; and I have a simple question:

在java程序包中,如果该类是程序包的一部分,则package语句必须在源代码文件中的第一行,然后再出现任何import语句.

In java packages, if the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present.

package语句的开头是否有任何有价值的理由?

Is there any valuable reason for the package statement should be at the beginning?

推荐答案

有原因吗?当然,语言设计师做出了必须在编译单元开始时进行的设计决策.来自 JLS :

Is there a reason? Sure, the language designers made a design decision that it had to be at the start of the compilation unit. From JLS:

CompilationUnit是Java程序语法(第2.3节)的目标符号(第2.1节).它是由以下产品定义的:

CompilationUnit is the goal symbol (§2.1) for the syntactic grammar (§2.3) of Java programs. It is defined by the following productions:

CompilationUnit:
    [PackageDeclaration] {ImportDeclaration} {TypeDeclaration}

因此,您必须有零个或一个包声明,然后有零个或多个导入声明,然后是零个或多个类型声明.

So, you've got to have zero or one package declarations, then zero or more import declarations, then zero or more type declarations.

会有所不同吗?绝对地;但事实就是如此.

Could it have been different? Absolutely; but it is the way it is.

这篇关于有什么理由应该将java中的package语句放在开头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆