Search code examples
javaclassclass-names

Should or Must in Java file name


I'm learning about Java and have just encountered 2 instructions:

  1. A class must have a matching filename
  2. Remember that the name of the java file should match the class name

Ask:

Should in (2) LIKE or DIFFERENT Must in (1) ?? -> (1), (2).

What is correct ??


Solution

  • As mentioned in the comments, this is not as clear as it might seem at first, partly because the JLS does not require packages to be stored in a file system:

    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:
    [...]

    So your answer is basically:

    It may be a "must", depending on your compiler.