package app; public class Museum1 { private static int visitorsSum; private String country, city, address; private double rating; private int visitors; /** * Empty constructor * @return */ Museum1() { } /** * Constructor with parameters (4 vars) * * @param country The country where the Museum * @param city The city where the Museum * @param address The address where the Museum * @param rating The rating where the Museum */ Museum1(String country, String city, String address, double rating) { this.country = country; this.city = city; this.address = address; this.rating = rating; } /** * Constructor with parameters (all vars) * * @param country The country where the Museum * @param city The city where the Museum * @param address The address where the Museum * @param rating The rating where the Museum * @param visitors The number of visitors */ Museum1(String country, String city, String address, double rating, int visitors) { this.country = country; this.city = city; this.address = address; this.rating = rating; this.visitors = visitors; setVisitorsSum(visitors); } /** * Setters block */ public void setCountry(String country) { this.country = country; } public void setCity(String city) { this.city = city; } public void setAddress(String address) { this.address = address; } public void setRating(double rating) { this.rating = rating; } public void setVisitors(int visitors) { this.visitors = visitors; } private static void setVisitorsSum(int visitorsSum) { Museum1.visitorsSum += visitorsSum; } /** * Getters block */ public String getCountry() { return country; } public String getCity() { return city; } public String getAddress() { return address; } public double getRating() { return rating; } public int getVisitors() { return visitors; } @Override public String toString() { return "Museum{" + "country='" + country + '\'' + ", city='" + city + '\'' + ", address='" + address + '\'' + ", rating=" + rating + ", visitors=" + visitors + '}'; } /** * Displays the number of all visitors */ static void printStaticSum() { System.out.println("Кількість відвідувачів за весь час: " + visitorsSum); } /** * Displays the number of all visitors */ void printSum() { System.out.println("Кількість відвідувачів за весь час: " + visitorsSum); } /** * Changes values of variables * * @param country The country where the Museum * @param city The city where the Museum * @param address The address where the Museum * @param rating The rating where the Museum * @param visitors The number of visitors */ void resetValues(String country, String city, String address, double rating, int visitors) { this.country = country; this.city = city; this.address = address; this.rating = rating; this.visitors = visitors; } } public class Main { //тут public static void main(String[] args) { // Initializing objects Museum1 arsenal = new Museum1(); Museum1 pharmacy = new Museum1("Ukraine", "Lviv", "Stavropigijska, 3", 4.5); Museum1 andreja = new Museum1("Ukraine", "Lviv", "Runok, 6", 5.0, 50); //Displays information on the screen System.out.println(arsenal.toString()); System.out.println(pharmacy.toString()); System.out.println(andreja.toString()); System.out.println(); Museum1.printStaticSum(); andreja.printSum(); } } 
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

The public class Main should be in its own file - it is written right in the error in human language, why not try to read what the compiler wants to tell you about its problems with your code.

This error means that in Java, each public class must be in a separate file, and the file name must match the class name. In a file with a public class can only be a private class or a nested class.

Related question: "How many public classes can be in one file?"

    Actually, the answer is simple; you cannot place two external public classes in one file. Split your code into two files (in each class) and this error will disappear.

    • five
      In one file, you can stir several classes, only public should be one (the one that corresponds to the file name). - post_zeew
    • four
      @post_zeew is not quite right, there can be any number of public classes, if they are inner or nested , and there is only one public class at the top level - Artem Konovalov
    • @ArtemKonovalov, I agree. I meant top-level classes. - post_zeew
    • I agree, I went to place my question and saw this not very strange question ... - plesser