Java Beginner question about String [] args in the main method

So I just tried to exclude the arguments String[]from the methodmain

Compiled!

But the JVM shows an exception

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

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

Having typed this, I thought that the compiler did not see it as a method main.. is that so?

If so ... why? I mean, shouldn't there be only one main method that MUST have an argument String[]as an argument?

+3
source share
13 answers

Having typed this, I thought that maybe the compiler did not see it as the main method .. is that so?

. , main. JVM " ", static public void main(String[]) , , .

, , : JAR , , " ".

+6

JVM .

public static void main( String[] args )

. main "" .

+6

public static void main(String[] args), , JVM. , public static void main(), , JVM, .

public static void main(String[] args), , , .java .

, public static void main(String[] args) ( ), , .

+4

" ", java , . - , jar , "" . -, -, "" . - .

, .

+4

Java . , , .

, java ClassName, Java ClassName.class public static void main (String[]) ( , String[]) . , Java :

"main" java.lang.NoSuchMethodError: main

+3

. , .

, , , . , main(), - .

+2

?

static void main() {
}

, JVM.

- , . , .

, (String []) , , - main_String_arr, main() - main.

+1

..

Java , main

, .
.!!

# - .

, , :

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

main, - . , String [] args.

+1

. , "main" int, :

public static void main(int foo){}

, ! , Java-, Java , "main", String.

+1

: . jar, ; , -. (String []) . .

, java main (String []); , . , , .

+1

Java/ :

Java , :

public static void main(String[] args)

public static (public static static public), public static, . , "args" "argv".

main C ++; , .

, main Java . - , main . , main, .

, varargs :

 public static void main(String... args)

, varargs Java .

.

+1

:

  • :

    public static void main(String[] args)
    
  • .

  • Several classes can contain the main method inside one compilation module (and therefore they will all be called executable classes)

  • A class containing the main method may or may not be publicly available.

  • By mistake, if you must omit the static keyword (or the signature is different in some way), compilation will succeed, but a runtime error will occur.

From my blog:

Java: important notes about the main method

0
source

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


All Articles