In Java why we write public static void main(String args[])
why not main()?

Answer Posted / gowrishankar

The Main method is the method in which execution to any java
program begins.
A main method declaration looks as follows:

public static void main(String args[]){
}


The method is public because it be accessible to the JVM to
begin execution of the program.

It is Static because it be available for execution without
an object instance. you may know that you need an object
instance to invoke any method. So you cannot begin execution
of a class without its object if the main method was not
static.

It returns only a void because, once the main method
execution is over, the program terminates. So there can be
no data that can be returned by the Main method

The last parameter is String args[]. This is used to signify
that the user may opt to enter parameters to the java
program at command line. We can use both String[] args or
String args[]. The Java compiler would accept both forms.

Command line argument
Any number of arguments can be passed to the java program
through command line. While running the program any things
written after the name of the class are the command line
arguments. Arguments are delimited by the space.
This sample code prints all the command line arguments to
the console.
public class CmdLn{

public static void main(String[] args) {
System.out.println("d");
for (int i = 0; i < args.length; i++)
System.out.println(args[i]);
}

}


Java CmdLn arg1 arg2 arg3 arg4
Output:
arg1
arg2
arg3
arg4



so,if we simply give the main() function program wont work
without the access specifier.

Is This Answer Correct ?    1 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How to declare an arraylist in java?

482


What is slash r?

545


What are the types of java languages?

519


When should I use a singleton?

561


Does printwriter create a file?

542






Why does java not support pointers?

554


What is the difference between checked exception and unchecked exception?

530


What method is used to specify a container's layout in java programming?

542


How do you initialize an arraylist in java?

513


How many types of memory areas are allocated by JVM in java?

597


Can an interface be final?

550


Can we create object of static class?

528


What is mnemonic in assembly language?

555


what are the states associated in the thread? : Java thread

593


If two threads have same priority which thread will be executed first ?

844