Java class 命名规则
Java class naming rules
在JAVA中,class名称必须始终与文件名相同,但有时文件包含多个class。文件中只有单个class(或接口)可以是public,并且必须与文件同名。但是,如果文件名有多个 class 不是 public 的文件名(或接口),如何确定?
interface Foo {}
class Bar{}
有些人对这个问题似乎很困惑
我实际上知道,无论我选择 Foo 还是 Bar 作为文件名,它都会起作用。然而,我感兴趣的是是否存在某种命名 class 的约定。
为什么我不随心所欲地命名呢?因为我实际上正在编写一个重构代码的应用程序,每当它重命名 classes 时,我需要知道如何以及何时更改我的文件名。
到目前为止我认为正确的方法是:
如果class有一个public节点,使用它的名字作为文件名,
否则只选择第一个节点,所以在这个例子中 Foo 会赢。所以我简化了这个问题:这是正确的方法,还是有更多的东西?
有 2 条规则要遵循:
第一条规则: class 可以有包(默认)或 public 可见性
第二条规则: 您定义为 public 的 class 必须在 .java 源文件中实现名称,但是 class 非 public 的名称可以在源文件中使用其他名称。
引用 Java 语言规范,第 7.6 节 Top Level Type Declarations :
If and only if packages are stored in a file system (§7.2), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java
or .jav
) if either of the following is true:
The type is referred to by code in other compilation units of the package in which the type is declared.
The type is declared public
(and therefore is potentially accessible from code in other packages).
This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a Java compiler to find a named class within a package. In practice, many programmers choose to put each class or interface type in its own compilation unit, whether or not it is public or is referred to by code in other compilation units.
因此,如您所见,并不像您所说的那样要求“class 名称必须始终与文件名相同”。
这只是一种允许一些编译器在编译期间轻松找到class源代码的方法。
但是,更重要的是,它还可以帮助人类找到源代码。如果您看到对 class com.example.Foo
的引用,您就知道在哪里可以找到它,因为它将在文件 com/example/Foo.java
.
中
非public(包私有)顶级classes,技术上可以放置在任何名称的文件中,多个这样的classes可以捆绑在一个文件中单个文件,但这使得它们很难找到。出于这个原因,我看到一条指南(不记得在哪里)说你应该总是把顶级 classes 放在他们自己的文件中,有一个例外:
- 如果非publicclass只被对方class使用,放在同一个编译中就可以了单位(.java 文件)与其他 class.
基本上这意味着您应该考虑任何顶级 class,其名称不是文件名,是“文件范围的”,即使它在技术上是包范围的。
在JAVA中,class名称必须始终与文件名相同,但有时文件包含多个class。文件中只有单个class(或接口)可以是public,并且必须与文件同名。但是,如果文件名有多个 class 不是 public 的文件名(或接口),如何确定?
interface Foo {}
class Bar{}
有些人对这个问题似乎很困惑
我实际上知道,无论我选择 Foo 还是 Bar 作为文件名,它都会起作用。然而,我感兴趣的是是否存在某种命名 class 的约定。
为什么我不随心所欲地命名呢?因为我实际上正在编写一个重构代码的应用程序,每当它重命名 classes 时,我需要知道如何以及何时更改我的文件名。
到目前为止我认为正确的方法是:
如果class有一个public节点,使用它的名字作为文件名, 否则只选择第一个节点,所以在这个例子中 Foo 会赢。所以我简化了这个问题:这是正确的方法,还是有更多的东西?
有 2 条规则要遵循:
第一条规则: class 可以有包(默认)或 public 可见性
第二条规则: 您定义为 public 的 class 必须在 .java 源文件中实现名称,但是 class 非 public 的名称可以在源文件中使用其他名称。
引用 Java 语言规范,第 7.6 节 Top Level Type Declarations :
If and only if packages are stored in a file system (§7.2), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as
.java
or.jav
) if either of the following is true:
The type is referred to by code in other compilation units of the package in which the type is declared.
The type is declared
public
(and therefore is potentially accessible from code in other packages).This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a Java compiler to find a named class within a package. In practice, many programmers choose to put each class or interface type in its own compilation unit, whether or not it is public or is referred to by code in other compilation units.
因此,如您所见,并不像您所说的那样要求“class 名称必须始终与文件名相同”。
这只是一种允许一些编译器在编译期间轻松找到class源代码的方法。
但是,更重要的是,它还可以帮助人类找到源代码。如果您看到对 class com.example.Foo
的引用,您就知道在哪里可以找到它,因为它将在文件 com/example/Foo.java
.
非public(包私有)顶级classes,技术上可以放置在任何名称的文件中,多个这样的classes可以捆绑在一个文件中单个文件,但这使得它们很难找到。出于这个原因,我看到一条指南(不记得在哪里)说你应该总是把顶级 classes 放在他们自己的文件中,有一个例外:
- 如果非publicclass只被对方class使用,放在同一个编译中就可以了单位(.java 文件)与其他 class.
基本上这意味着您应该考虑任何顶级 class,其名称不是文件名,是“文件范围的”,即使它在技术上是包范围的。