I\'m fairly new to Java and trying to figure out how to solve the following error:
CalculatorWithMemory.java:1: class Calculator is publi
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.
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
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
.
You need to name the file containing the class Calculator.java
class Something {
}
instead of
public class Something {
}
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.