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 / ankur

in java we write public static void main(String args[])
instead of just main because:....

public.....The main method must be public because it is
call from outside the class. It is called by jvm.

static....The main method must be static because without
creating an instace jvm can call it. If the main method is
non static then it is must to create an instance of the
class.

void....Main method doesn't return anything therefore it is
void also.

(String args[])....It is used for receiving any arbitirary
number of arguments and save it in the array.

Is This Answer Correct ?    429 Yes 20 No

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

Answer / ravi

static because once the class is loaded the main() will be
invoked first.
void because main()is not returning any value.

Is This Answer Correct ?    228 Yes 99 No

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

Answer / sumalatha

yes in every progrm in jave the main is static.because when
the program will start ,thit means runnig then the main is
also one method it was called first that means without
instans creations the main is called.thet is the reason we
are put as main is static method(static is used for without
instance creat u call it).void means it does not reaturn
any value

Is This Answer Correct ?    148 Yes 43 No

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

Answer / sanjay gupta

public static void main()

1> Main() is the entry point of the program. Rather we can
say it is the main gate to enter inside the apartment. It
should be public because the compiler need to enter inside
the method (The guest need to access the apartment to reach
the security guard).

Public--> Compile whos is from another instance need to
access this method.

Statis --> Since main method is written inside a class and
to access a method of a class we need to create an object
of that class first. Since main() is the method which
compiler nned to call before creating any object of that
class we need to declare main as static. Static methods can
be called directely by using the class name. Thats why the
neame of the file should be same as the name of the class
contain main() method.

Void--> Since the method can return any type of valu or it
might not return anything also so the comiler is designed
in such a way that it should not take any return value so
we declare main as void type.

For further information please reach me at
sanjaygupt1011@gmail.com

Thanks~
Happy Programming...

Is This Answer Correct ?    88 Yes 12 No

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

Answer / anjani kumar jha

Use of Public-----There is always only one main class that
is one file having only one main file. Since that main file
is used anywhere means might be in another package so there
is a public use of keyword that is public keyword is
required for main function

Static:-----Since compiler in java always look the main
function and when compiler always calls the main so main
class is loaded in first time call for that static keyword
is used.
you can use
1)public void main(String args[])
it compiles file but wont run\

2) void main(String args[])
it compiles file but wont run

3)void main(String args[])
it compiles file but wont run

Is This Answer Correct ?    87 Yes 37 No

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

Answer / manikandan

in java , the entry point for execution is Main(),

it is tha first method to be invoked .

so PUBLIC is declared for no duplicates should occur and for
global declaration .

VOID , the main method will not return any value . So void
is declared .

Finally STATIC , in java everything is accssed through
objects . But here main() is the first method to be invoked
. STATIC is defined for this purpose , If a method is
declared as STATIC it can called without any objects
reference .

So the Main() in java is defined as

public static void main(String args[])

Is This Answer Correct ?    50 Yes 15 No

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

Answer / angela

when we run java program, jvm internally calls "main" method.
jvm is built in such a way that it searches for the entire
signature of "main" method i.e public static void
main(String a[])

Example program :
----------------------
class Demo
{
public static void main(String a[])
{ }
}

String a[] -> is used to pass values at runtime.
(c:/>java Demo hai)

public -> providing accessibility for outside code (jvm).

static -> without creating an object calling "main" method
with the class name

c:/>javac Demo.java
c:/>java Demo
(here java internally calls jvm and passes class name
Demo, then jvm loads Demo class and calls Demo.main()
without creating object )

void -> does not return any value because jvm simply calls
main() method and does not have a variable to assign return
value.

Is This Answer Correct ?    23 Yes 1 No

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

Answer / prabhakant prabhakar rasal

We write public because to access main() method outside the
class.
Why static because to invoke main() method before creating
any object, so it simple to compiler that to identify the
main() method.
void, it represents that it does not returns any value.
string args[]
There are two main reasons to write string args[]
1. TO identify the program
2. To get command_line arguments

Is This Answer Correct ?    27 Yes 13 No

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

Answer / sivadasan

1. public - Access Specifier, properties and methods can
access anywhere, any package.

2. main() - The Java environment starts the execution of a
program from main() method.

3. void - The main() method is not return anything. So the
return type of the main() method must be void.

4. static - The jave environment able to call the main()
method without creating an instance of the class. So its
declared as static

5...(String args[]) -- In java, whatever we give, it will
takes as String. So we are giving the command line argumet
as String.


i hope it will help u..

Shivadasan
Coromandel Infotech

Is This Answer Correct ?    16 Yes 4 No

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

Answer / suresh.t.c

public static void main(String arg[])

public for accessing all object

static for to call main method without instance

void main for not returning values for avoid compilation errors

String arg[] is ,in input takes as strings so takes multiple inputs declared String with array of arg[]

Is This Answer Correct ?    14 Yes 3 No

Post New Answer

More Core Java Interview Questions

What are the ways of polymorphism other than Overridding & Overloading

1 Answers  


C and C++ has constructors and distructors, why does Java does not have distructors?

1 Answers   T3 Softwares,


Are variables stored in ram?

0 Answers  


What is static method with example?

0 Answers  


Why stringbuilder is not thread safe?

0 Answers  






What do you mean by buffering?

0 Answers  


What is printwriter in java?

0 Answers  


Is java a security risk?

0 Answers  


can we create a instance for intwerface?

3 Answers  


What are different ways of object creation in java ?

0 Answers  


Why array is used in java?

0 Answers  


What is the use of jtable?

0 Answers  


Categories