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

Answers were Sorted based on User's Feedback



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

Answer / priyanka

DECRIPTION ABOUT PUBLIC:
The main method should be acceseible to every function
which is inside the package and outside the package. and it
is possible only if main method is declared as public

DECRIPTION ABOUT STATIC:
Static is a storage class. static allocates space for
its data and it is first step done after compilation. In
java main method is coded inside a class.. but compiler
need to call the main method first as it is one of the
protocol in ANSIC..
A method in the class without creating an instance can
be called only if the method is static.. so main method
should be static

DECRIPTION ABOUT VOID:
As main method returns nothing or empty main is declared
as void

DECRIPTION ABOUT STRING(ARGS[]):
In java every input is considered as string. even the
input is of integer type it is taken as string. so main
takes arguments of type string.

Is This Answer Correct ?    9 Yes 0 No

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

Answer / ashish jindal

public static void main(String ashish[])
1. PUBLIC:- it is a access modifier through which a class or
a data member can be accessed anywhere. Main Class is Public
because it is called explicitly by Java Virtual Machine.

2. Static:- Static is used to call the main without creating
object. If Static is not used then it will behave like a
instance member function and instance member functions can
change the properties of its own object's members and functions.

3. Void:- Main do not returns any value.

4. String:- In java everything is accepted as a string.

Is This Answer Correct ?    7 Yes 0 No

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

Answer / shreya

main method is the starting point of our program
execution.and it calls first outside the program so that we
have to specified it public.public means access from
anywhere.
Java is strictly object orinted programming language and we
can access any method by using the object.but this is first
method rite now we don't have any object so that static
keyword is used.We can use static method without having the
instance of this class.
void means this function doesn't return anything.
String args[] specified the string array it is use to take
compile time arguments.It takes only String type argument.
There is one question arise if we don't want to pass the
argument although why we use String args[].Answer is
runtime calls always method with String argument but you
can overload main method....

public class Simple
{
public static void main()
{
System.out.println("My Main");
}
public static void main(String args[])
{
Simple.main();
}
}

when you run this program your runtime calls main method
with string arguments first.and then call main method
without arguments and the output will be "My Main".

Is This Answer Correct ?    7 Yes 3 No

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

Answer / deependra prarap singh

in the java when we creat any of the class then the "java's compiler after entering in the class ,
fistly search for the static because the java's compiler know every thing about the static
and then java's compiler search for the main method,
as we know that the main is a method then there should be some return type so we use void because it has no return type
after declaring the void now we use the public because we are to access the main method from any where or outside
"

Is This Answer Correct ?    5 Yes 1 No

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

Answer / suryanarayan jena

Because JVM is static.

Is This Answer Correct ?    4 Yes 0 No

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

Answer / srinu

public static void main(String args[]) the meaning is

public:- Actually the main()method will be called by JVM .so
JVM is located other place so JVM will called by our main()
its public

static: with our object creation run our main()method so
thats its static

void:our main() method can't any return type thats its void

String args[]:. To get command_line arguments

Is This Answer Correct ?    7 Yes 4 No

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

Answer / prasu

public means this method is called from out side of the
class, means we create the object out side of the class that
situation method is available to object.


viod means return type of the method is nothing.the caller
of main() i.e jvm can not recive any thing from called main().


Static means when ever the class is loaded into memory first
static block is executed so,first jvm calls main().

string args[] is an array,it will receive string type of
arguments.



why we write only that type of specification is because jvm
knows only that type of specificatroin.

Is This Answer Correct ?    4 Yes 1 No

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

Answer / kanchan bangar

becoz in java programs execution is starts from main method
and static method is for the first preference
method.ppublic is for the global scope to all over the
progarms..............

Is This Answer Correct ?    2 Yes 0 No

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

Answer / b.prasad

1.we should give public bcoz the main() used my all
applications that r out side of application
2.we should give static bcoz it is instance method.so that
with out creating an object we can call it directly.and the
definition of the method should not been changed
3.we should give void bcoz the main() does not return any
value.ie void means return nothing
4.we should give args[],bcoz the main method takes some
arguments in the command prompt that are the string type and
also the jvm defines the signature of the main() is like that

Is This Answer Correct ?    3 Yes 1 No

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

Answer / manik kumar bardhan

public means access specifier
static means
in general we always start execution from main()
but in java main() exist with in a class so when ever we r
executing any method which r within the class we need
instance variable to execute that method(a.print()) but here
without creating any instance main method will execute first
b/c static is the keyword which used for method then method
become a associated with class.for execution of that method
no need create instance.
void :return type
main is the method where java compiler starts execution.

Is This Answer Correct ?    3 Yes 1 No

Post New Answer

More Core Java Interview Questions

what is the major difference between linkedlist and arraylist in java?

0 Answers   IBS,


Can we return null in java?

0 Answers  


What is the concept of multithreading?

0 Answers  


What is static data type in java?

0 Answers  


What does system.gc() and runtime.gc() methods do?

0 Answers  






What’s the difference between the methods sleep() and wait()?

0 Answers  


Give us the name of the list layoutmanagers in java?

0 Answers  


What is final method?

0 Answers  


whats the purposr of using serialization?

6 Answers  


Explain how hashmap works?

0 Answers  


why Interface used?

0 Answers   HCL,


How are the elements of a gridbaglayout organized?

0 Answers  


Categories