Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm writing my first ever java program and it's not compiling for a certain reason

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading