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

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the types of arrays in java?

561


Differentiate between postfix and prefix operators in java.

646


What is regex java?

561


How do you read and print a string in java?

528


what is thread in Java ?

629






Can constructor return value?

512


Can we create object of inner class in java?

495


Which way a developer should use for creating thread, i.e. Sub classing thread or implementing runnable.

522


Is integer passed by reference in java?

570


What is sizeof () operator?

573


I want to persist data of objects for later use. What’s the best approach to do so?

507


What initialize variables?

505


Is stringwriter thread safe?

557


What happens when you invoke a thread’s interrupt method while it is sleeping or waiting?

578


What is jrmp?

550