60 435
Assignments Done
98,1%
Successfully Done
In April 2018

Answer to Question #4260 in Java | JSP | JSF for manjunath

Question #4260
Hi,
I'm new to java programming and i need the explanation why we use
public static void main(String args[])
and why we use "main" only rather than other word...please help me....
Expert's answer
The word "main" comes from language C.
The program in C/C++ can be regarded
as a collection of functions and declarations of variables, structures and
classes.
Moreover, each structure/class contains declarations of its
properties (internal variables) and methods (internal
functions).
If we
want to "run the program", we should decide which function should be runned
first.
Therefore the developers of C just fixed up that the first function to
run will be called "main" (so to say "primary"
function) and this function
has very special declaration either

void main( int argc, const char*
argv[] )
{
& ..........
}

or

void
main()
{
& ..........
}


The first declaration means
that the program will take
& argc - command line arguments which will be
written into the array of C-strings& argv[].

The second declaration means
that the program do not recognize any command line arguments.


In Java
the situation is similar.
The program in Java consists of descriptions of
classes ONLY, while each class contains its own properties ("variables")
and
methods ("functions").
So, again in order to run the program we should decide
which method of which class will be runned first.

Therefore the
developers of Java fixed up that in the program
there should be a unique
public class which contains a public method called "main" and this function
should
have the following declaration:

public static void
main(String args[])
{
& ...............

};


Similarly to the argument& "char * argv[]" of the C/C++ function
"main"
the argument String args[]& is an array of Java-strings.
On the
other hand the length of this array (the argument "int argc" in C/C++ "main") is
absent, since the Java-array is
a class which has property length:
&
args.length
gives the length of args.

The keyword "void" means that
the method "main" does not return any value.

The keyword "public" means
that this method can be accessed outside from class.

The keyword "static"
means that this method uses no instance variables of any object of the class in
which it is
declared.
For example if you write

public class
A{
int a; & // instance variable
public static void main(String
args[])
{
& a=5; & // error: static method "main" uses instance
variable "a"
};
};

the compiler will give an error.

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions