How to define main (String [] args) in Java without receiving warnings and errors?

When I create a new main.java file in the default package in my Eclipse project, it generates a main method that looks like this:

 public static void main(String[] args) { } 

This immediately raises a warning saying This method has a constructor name . A suggested fix is ​​to remove void :

 public static main(String[] args) { } 

Now, and not a warning, I get an error message: Illegal modifier for the constructor in type main; only public, protected & private are permitted Illegal modifier for the constructor in type main; only public, protected & private are permitted . If I remove static , my code now looks like this:

 public main(String[] args) { } 

This time I still get an error message, but something else:

 Error: Main method not found in class main, please define the main method as: public static void main(String[] args) 

Argggh! But that brings me back to where I started. How to define the main method so that I don't get any errors or warnings?

I am using Eclipse Juno Service Release 2 and JavaSE-1.7. Please note: I am very new to Java; I come from C # background. Also, this is probably a recurring question, but I cannot find it.

+6
source share
4 answers

Do not call your main class, but main .

In general, stick to Java coding standards: start class names with capital ( main instead of main ), and you won't run into these problems.

+13
source

If you name the main.java file, the class should also be called main , which is against the standard (classes start with a capital letter), but it is possible. In a class, a method named just like a class is considered a constructor. Therefore, to fix your problem and comply with the standard, rename your class and file to main using capital "M"

+5
source

Change the name of your class from main to main or to another. In addition, following the specification of the JavaBean API , your classes should be in CamelCase with the first letter in the capital.

Not 100% related to the question, but you also should not create classes with Java JDK class names like String :

 public class String { public static void main(String[] args) { System.out.println("Try to execute this program!"); } } 

This will not only create problems for the / JVM compiler, but also for future readers (remember that you are also a reader of your own code).

Note. To fix the code above, simply refer to the java.lang.String class using its full name:

 public class String { public static void main(java.lang.String[] args) { System.out.println("Try to execute this program!"); } } 

Or even better, change the class name.

+3
source

In java, the class name and file name must match. If you have a file called main.java, then the class name should also be the main one, in which case the constructor method will be called main, so you cannot have the main method.

Change the file and class name to something other than the main one.

+1
source

Source: https://habr.com/ru/post/943715/


All Articles