What does `public static void main args` mean?

I'm not sure what that means, whenever you write code, people say it

public static void main(String[] args) { 

What does it mean?

+5
source share
5 answers

Here is a somewhat detailed explanation of why the main method is declared as

 public static void main(String[] args) 

The primary method is the entry point of the Java program for the Java Virtual Machine (JVM). Let them say that we have a class called Sample

 class Sample { static void fun() { System.out.println("Hello"); } } class Test { public static void main(String[] args) { Sample.fun(); } } 

This program will be executed after compilation as java Test . The java command will launch the JVM and load our Test.java class into memory. Since the main entry point to our program, the JVM will look for the main method declared as public , static and void .

Why should main be declared open?

main() must be declared public , because, as we know, it is launched by the JVM when the program starts, and the JVM does not belong to our program package.

In order to access the main external side of the package, we must declare it as public . If we declare it as something other than public , it will display a Run time Error , but not a Compilation time error .

Why should main be declared static?

main() must be declared as static because the JVM does not know how to create a class object, so it needs a standard way to access the main method, which is possible by declaring main() as static .

If a method is declared static , we can call this method outside the class without creating an object using the syntax ClassName.methodName(); .

So the JVM can call our main method as <ClassName>.<Main-Method>

Why should main be declared invalid?

main() must be declared invalid because the JVM does not expect a value from main() . Therefore, it must be declared as void .

If a different return type is provided, this is RunTimeError ie, NoSuchMethodFoundError .

Why should main have string array arguments?

main( ) must have String arguments as arrays because the JVM calls the main method by passing a command-line argument. Since they are stored in a string array object, it is passed as the argument to main() .

+15
source

According to the Java language specification, the execution of a Java program begins with the main () method. The main () method must follow the specific syntax, it can be explained as follows:

 public static void main(String[] args) 

public - The access specifier indicates that main() is available for all other classes.

void - return type, main() returns nothing.

String args[] - arguments to the main() method, which should contain an array of type string.

static - access modifier. The main method should always be static, because the main () method can be called without instantiating the class.

Suppose we are running the Helloworld Java program.

While the program is running, we use the command

 java Helloworld. 

Inside, this command is converted to

 Helloworld.main() 

Using the static main() method, the JVM calls the main() method without first creating the object.

+1
source

public โ†’ Access Specifier. Any other class can access this method.

static The method is bound to a class, not to an instance of the class.

void โ†’ Type of return. The method returns nothing.

main(String[] args) โ†’ the name of the main() method. It takes an array of String as an argument. Arguments String[] are command line arguments.

Note. The main() method defined above is the entry point of the program, if you change the signature, then your program may not work.

0
source
  • Public = This method is available for all other classes.

  • static = This method does not require an instance to start.

  • void = This method returns nothing.

  • main() = The main method (the first method to start).

  • String[] = An array of strings.

  • args = The name of the array.

0
source

In Java, your main method should always be:

 public static void main(String args[]) 
  • Program execution begins with the main() function, therefore, the main() function.

  • It must be public so that it is accessible to the external environment.

  • The main() method is always static, since you know that program execution starts with the main() method, and there is no instance of the class containing the main() method. Therefore, since the static method can be run without any instance, it is declared static.

  • Java is platform independent, so you can try to compile a java file on one system and try to execute the class file on another. The bit architecture of each machine can be different, so the return type of the main function should always be main() .

Hope this helps.

0
source

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


All Articles