This is the code i have tried running
public class main
{
public static void main(String[] args)
{
System.out.println("Hello, world!");
}
}
and it gives me this error:
tempCodeRunnerFile.java:1: error: class main is public, should be declared in a file named main.java
public class main
i don’t know what wrong tbh i have tried modifying and it looks similar to mee likethe example they gave..
>Solution :
basically the compiler tells you whats wrong. It says, "class main is public, should be declared in a file named main.java".
Java has a naming rule, that a class inside a java file needs to match that file name.
Example 1:
Filename -> File.java
inside that file:
public class Main{
...
does violate that rule (class name does not equal the file name)
Example 2:
Filename -> Main.java
inside that file:
public class Main{
...
follows that rule (class name does equal the file name)
Also for beginners:
This is a good quick read for basic code / naming conventions.
https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html