What does `public static void main args` mean?

后端 未结 5 1252
不知归路
不知归路 2020-11-30 10:44

I am not sure what this means, whenever before you write a code, people say this

public static void main(String[] args) {

What does that m

5条回答
  •  再見小時候
    2020-11-30 11:32

    In Java your main method must always be:

    public static void main(String args[])
    
    1. The program execution starts with main() function, hence the main() function.

    2. It must be public so that it is accessible to the outside environment.

    3. The main() method is always static because, as you know that the program execution starts at main() method and there is no instance of the class containing main() method is initiated. Hence as the static method can run without need of any instance it is declared of static.

    4. Java is platform independent, hence you may try to compile the java file on one system and try to execute the class file on another. Each machines' bit architecture may be different hence the return type of the main function must always be main().

    Hope this helps.

提交回复
热议问题