Java Error: Should be declared in a file named

前端 未结 6 1922
说谎
说谎 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条回答
  •  猫巷女王i
    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.

提交回复
热议问题