Learning Java from the textbook by C. Sierra and B. Bates "Learning Java 2nd Edition 2012", there is an example of a program code consisting of three classes, all three classes have the prefix public, but when I rewrote the code and tried to run, the command line issued something like "the name of your public class must match the name of the file". Since the name of the file is one, and I have assumed three classes of classes that there can be only one public class. As a result, I left public only for the class with main. It worked, but now I don’t know if I was right about the fact that there can be only one public class in the program? But then, does this mean that there is a mistake in the textbook? Or I have some other Java, which has changed since the release of the textbook in 2012. I can attach screenshots from the textbook, if necessary.

I write the code in Notepad ++, I launch programs via the Windows command line, if that matters.

  • In one file there can be many classes, but the one that is named as a file should be public. The rest of this file - without public - Alexey Shimansky
  • If you are talking about the three classes on page 69 -70, then at the end of the 68th page it is written: Classes: GuessGame.class, Player.class, GameLancher.class - this means that these are three files, but the emphasis is not on this and the newcomer can to be incomprehensible, I agree. - pavlofff
  • I met this reading books. I do not know what the author wanted to say. Most likely the fact that each class is in a separate file. - DmitriyKhirniy
  • @pavlofff about them. I don’t know how three files can be meant there, up to this point the reader is led by the hand from the very beginning, and I don’t remember that they even taught there to run a program consisting of several files. I have no idea how to do this. Apparently a mistake in the book after all. - user212265
  • A file containing the public main() method main() entry point) is launched, the rest happens without your participation, the compiler will find the files it needs. - pavlofff

2 answers 2

In one physical java-file there can be only one public class, whose name must match the file name and any number of nested classes.

It is also allowed to place separate non-nested classes with the default/package/private access level in the same physical file, but this is not recommended.

In a Java program, there can be any number of physical java files, each of which can contain a public class. All these classes can be called directly from any other class if they are in the same scope (or through import, if in different areas).

  • "with access level private" - the top level of a class can have only public or package-private (without access modifier) - Russtam
  • @Russtam Yes, I meant that together with the public-class there can be private ones, now I will correct it. - pavlofff
  • Specify which of these restrictions are strictly prescribed in the standard and are checked by the environment, and which are just the rules of good form? - Kromster
  • @Kromster is where it is written that it is allowed, but not recommended, then the compiler will skip, but this is not done, and the rest is not compiled. - pavlofff
  • one
    @Kromster yes, the compiler will issue a warning about an error if there is more than one public class (read the question), it will also issue a warning if the file names and public class will not match. If development is done in the IDE, then it also immediately marks the error before the compilation begins. Compiling with such an error will be impossible until eliminated. - pavlofff

Probably, the book meant that all these three classes are placed in different files.

  • No, programs with multiple files have not yet been given. I generally almost at the beginning of the book. - user212265
  • @KirovReporting This in Java goes without saying. I propose to compare with the text of the original book. - free_ze
  • Found the original, there is exactly the same code, here, you can see for yourself, pages 39-40. opus-college.net/devcorner/HeadFirstJava2ndEdition.pdf - user212265