Java Beginner question about String[] args in the

2019-04-12 00:49发布

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 ?

标签: java main
13条回答
家丑人穷心不美
2楼-- · 2019-04-12 01:24

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.

查看更多
相关推荐>>
3楼-- · 2019-04-12 01:25

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.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-04-12 01:25

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

查看更多
smile是对你的礼貌
5楼-- · 2019-04-12 01:29

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

查看更多
乱世女痞
6楼-- · 2019-04-12 01:29

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.

查看更多
7楼-- · 2019-04-12 01:30

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.

查看更多
登录 后发表回答