tags:

views:

200

answers:

12

So I just tried excluding String[] args from the main method

It compiled alright !

But JVM is showing an exception

Why did it compile when String[] args HAS to be included every time ?

What is going on here ? Why won't it show a compilation error ?

typing this made me think that may be compiler did not see it as THE main method ..is that so ?

If that is the case..why not ? I mean isn't there supposed to be just one main method that MUST have String[] args as the argument ?

+5  A: 

The JVM is looking for a very special main method to run. Only the signature

public static void main( String[] args )

is found. All other methods with name main are just "normal" methods.

tangens
Why won't compiler detect the error that main does not have String[] args ?
Serenity
Because you don't have to have a `main` method. You are free to define one that the JVM shouldn't take to run the program.
tangens
Because you are allowed multiple instances of methods with the same name but with different parameters - it is called "overloading".
Kamikaze Mercenary
+1  A: 

Isn't that overloading? It's fully legal to define a method

static void main() {
}

It's just not the entry point the JVM will be looking for.

Overloading is the ability to have multiple methods with the same name but different arguments. The compiler in fact creates a name based on the method name and the arguments.

So a main(String[]) would be called, to the compiler, something like main_String_arr, and main() would be called something like main.

extraneon
+1  A: 

Yes..

The Java compiler will look for the same method signature to consider it a main

Writing any function that has the same name but another parameters will result in overloading functions..
Overloaded functions are not the same..!!

The case in C# is somehow different..

At last, you must make sure that your main is like that:

 public static void main(String[] args) 
Betamoo
+1  A: 

You can have many methods named main, but only one can be THE main one - entry point to the program. It is the one with String[] args.

Anton
oh I see..I ll just try running a program with two mains then
Serenity
+2  A: 

You are correct. The runtime is looking for a main method that takes a string array as a parameter, and isn't finding it.

The fact that you have a main method that doesn't take a string array is irrelevant. just like any other method, you can create multiple versions of main() that take different parameters - the runtime will just ignore them.

Eric Petroelje
+1  A: 

It's still a valid method. For example, you could have a static method called "main" with an int parameter if you wanted:

public static void main(int foo){}

The compiler doesn't complain because it's a valid method! It's just that, in order to run a Java program, Java looks for a static method called "main" with a single String array argument.

Michael Angstadt
+3  A: 

Java supports method overloading. This means you can have several methods with the same name, but different arguments.

Having said that, when you run java ClassName, Java looks in ClassName.class for a method with the signature public static void main (String[]) (it doesn't care what the String[] variable is named) and runs it. If it doesn't find one, Java will bomb out with the following exception:

Exception in thread "main" java.lang.NoSuchMethodError: main

R. Bemrose
+4  A: 

typing this made me think that may be compiler did not see it as THE main method ..is that so ?

Correct. There is no compile error because you're perfectly free to have all kinds of methods named main. But when you start the JVM and give it a "main class", then it will look for a method static public void main(String[]) in that class, and if it does not find such a method, it aborts with an exception.

This allows you to have multiple main methods in your program and is really the only thing that makes sense if you think about it: Applications can be composed from classes and JAR files from lots of different sources, written by different people at different times, so in many cases you can't really have a single, designated "main class" right from the start.

Michael Borgwardt
+1  A: 

You can use the compiler to compile part of an application: e.g. to make a jar file which you will call from another part of the app; or an applet that is started in a different way. Therefore the compiler cannot complain about the lack of a main(String[]) method. But trying to run the results of such a compile.

When you try to run the results of such a compile java is always looking for a specific main(String[]); if it can't find it it will throw a runtime exception. The main used to start an app must have exactly that signature.

DJClayworth
+3  A: 

Not every class has to have a public static void main(String[] args) method, only the one you want to run from the JVM. The result is that your class will compile with no error if it finds public static void main() but will throw an exception if you try and run it with the JVM because it can not find the entry point of the program.

Bottom line the entry point of your program must be public static void main(String[] args) which must be located in at least one of your .java files.

Note you can have multiple public static void main(String[] args) methods in your code (one per class) the advantage is you can use these to test and debug your classes individually.

Covar
+3  A: 

To try to answer "why is it legal to compile without a proper main method" it's because not every java project is a stand alone application that can be run. Some are just libraries, where other programs will include them as jar files and use their code, but they don't "run" themselves. Others may be web applications, where they are deployed onto a web server that has already been started, only the server itself actually has a proper "main" method. The web application project is opened up and executed by it.

The compiler didn't really know at compile time that you were intending to try to run your code as a stand along application.

Affe
+1  A: 

Here's a quote from Java Tutorials/Getting Started:

In the Java programming language, every application must contain a main method whose signature is:

public static void main(String[] args)

The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above. You can name the argument anything you want, but most programmers choose "args" or "argv".

The main method is similar to the main function in C and C++; it's the entry point for your application and will subsequently invoke all the other methods required by your program.

So to be precise, the main method is defined by Java as an application entry point. Not everything is an application, and not every main method is an application entry point. Applets, for example, don't need a main, because it's started with a different mechanism.

Note also that since the introduction of varargs, you can actually declare your application entry point as follows:

 public static void main(String... args)

This works because underneath the syntactic sugar, varargs in Java is implemented as arrays.

See also

Related questions

polygenelubricants