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:10

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.

查看更多
迷人小祖宗
3楼-- · 2019-04-12 01:13

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.

查看更多
对你真心纯属浪费
4楼-- · 2019-04-12 01:15

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) 
查看更多
beautiful°
5楼-- · 2019-04-12 01:17

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.

查看更多
Explosion°爆炸
6楼-- · 2019-04-12 01:20

Important points:

  1. Following is the signature of the main method:

    public static void main(String[] args)
    
  2. main method can be overloaded.

  3. Multiple classes can contain the main method inside a single compilation unit (and so all of them will be called executable classes)

  4. The class containing the main method may or may not be public.

  5. By mistake, if you were to omit the static keyword (or the signature differs in any way), compilation would be done but a runtime error will occur.

From my blog:

Java: Important points about the main method

查看更多
对你真心纯属浪费
7楼-- · 2019-04-12 01:22

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.

查看更多
登录 后发表回答