-6
class Blog{
    public static void main(String args[]){
        System.out.println("overflow");
    }
}

このファイルを First.java という名前で保存し、コンパイルすると、Blog.class でファイルが生成され、出力が得られます。

overflow

以下に示すように、同じプログラムを書いている場合:-

public class Blog{
    public static void main(String args[]){
        System.out.println("overflow");
    }
}

コンパイル後にエラーが発生します

   First.java:3: error: class Blog is public, should be declared in a file named Blog.java
4

5 に答える 5

0

From the JLS

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.

于 2013-09-18T18:14:58.650 に答える
0

Java クラスは常に、.java 拡張子が付いたファイル名 (大文字と小文字を区別) と同じ名前にする必要があります。したがって、Blog.java を保存する必要があります。

于 2013-09-18T17:53:40.317 に答える