Java Error: Should be declared in a file named

前端 未结 6 1765
说谎
说谎 2020-12-31 23:21

I\'m fairly new to Java and trying to figure out how to solve the following error:

Error reads

CalculatorWithMemory.java:1: class Calculator is publi         


        
相关标签:
6条回答
  • 2020-12-31 23:26

    If you want more than one class in the same file then you are supposed to be writing only one class to be public which has same name as file.

    All the other classes you can define as "class abc{}" or using other modifier then public.

    0 讨论(0)
  • 2020-12-31 23:29

    The name of the public class containing main must be same as the file name, in your case the file name should be Calculator.java

    0 讨论(0)
  • 2020-12-31 23:30

    As the error message implies, if you declare a class as public, it needs its own .java file. If you don't want to do that, then don't define it as public.

    0 讨论(0)
  • 2020-12-31 23:35

    You need to name the file containing the class Calculator.java

    0 讨论(0)
  • 2020-12-31 23:37
    class Something {
    
    }
    

    instead of

    public class Something {
    
    }
    
    0 讨论(0)
  • 2020-12-31 23:45

    This error occurs when the class name and the filename of a given Java program do not match. For example, say that the following program is saved in a file named Foo.java:

    public class Bar {     
        public static void main(String[] args) {
            System.out.println("Hello, world!");
        }
    }
    
    
    
    1 error found:
    File: Foo.java  [line: 1]
    Error: class Bar is public, should be declared in a file named Bar.java
    

    Since Foo does not match with Bar, the code doesn't compile. To fix this error, either rename the file or change the class name.

    0 讨论(0)
提交回复
热议问题