Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of 'String args[]' and static in 'public static void main(String[] args)' [duplicate]

How can you explain very well, to a beginner, the meaning of String args[] and the use of static in the following excerpt?

class FirstApp {
    public static void main(String[] args) {
        ...
    }
}
like image 920
Sam Avatar asked Aug 14 '12 13:08

Sam


People also ask

What does public static void main String [] args means?

I would break up public static void main(String args[]) in parts: public. It means that you can call this method from outside of the class you are currently in. This is necessary because this method is being called by the Java runtime system which is not located in your current class.

Is String args [] and String [] args same?

String args[] and String[] args are identical. In the sense that they do the same thing, Creating a string array called args. But to avoid confusion and enforce compatibility with all other Java codes you may encounter I'd recommend using the syntax (String[] args) when declaring arrays.

What is the difference between public static void main String [] args and public static void main String args [])?

There is no difference between the two, aside from personal preference.

What is public static void main String [] args throws IOException?

it means, the main method is not catching any exceptions, instead it handles the IOException by throwing it to the source which invoked the main method. You throw Exception if you want it to be handled by higher function or calling function.


4 Answers

I would break up

public static void main(String args[]) 

in parts:

public

It means that you can call this method from outside of the class you are currently in. This is necessary because this method is being called by the Java runtime system which is not located in your current class.


static

When the JVM makes call to the main method there is no object existing for the class being called therefore it has to have static method to allow invocation from class.


void

Java is platform independent language and if it will return some value then the value may mean different things to different platforms. Also there are other ways to exit the program on a multithreaded system. Detailed explaination.


main

It's just the name of method. This name is fixed and as it's called by the JVM as entry point for an application.


String args[]

These are the arguments of type String that your Java application accepts when you run it.

like image 151
Confuse Avatar answered Oct 08 '22 20:10

Confuse


I would point a beginner to the Wiki article on the Main function, then supplement it with this.

  • Java only starts running a program with the specific public static void main(String[] args) signature, and one can think of a signature like their own name - it's how Java can tell the difference between someone else's main() and the one true main().

  • String[] args is a collection of Strings, separated by a space, which can be typed into the program on the terminal. More times than not, the beginner isn't going to use this variable, but it's always there just in case.

like image 39
Makoto Avatar answered Oct 08 '22 20:10

Makoto


public static void main(string [] args)

public -its the access specifier means from every where we can access it; static -access modifier means we can call this method directly using a class name without creating an object of it; void- its the return type; main- method name string [] args - it accepts only string type of argument... and stores it in a string array

like image 24
user3830480 Avatar answered Oct 08 '22 20:10

user3830480


  • public : it is a access specifier that means it will be accessed by publically.
  • static : it is access modifier that means when the java program is load then it will create the space in memory automatically.
  • void : it is a return type i.e it does not return any value.
  • main() : it is a method or a function name.
  • string args[] : its a command line argument it is a collection of variables in the string format.
like image 24
Aishwarya Hungund Avatar answered Oct 08 '22 18:10

Aishwarya Hungund