aspose file tools*
The moose likes Beginning Java and the fly likes Why main() in java is declared as public static void main? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why main() in java is declared as public static void main?" Watch "Why main() in java is declared as public static void main?" New topic
Author

Why main() in java is declared as public static void main?

Priyanka Dandekar
Ranch Hand

Joined: Aug 06, 2008
Posts: 52
Why main() in java is declared as public static void main?

This question was asked to my friend in her interview.

I am looking for a well organized answer on this question, which can be used in an interview, can someone help.


Priya - Mockup Tool | Java | Struts2 | Lucene
Sachin Joshi
Ranch Hand

Joined: Aug 06, 2008
Posts: 83

Public - main method is called by JVM to run the method which is outside the scope of project therefore the access specifier has to be public to permit call from anywhere outside the application

static - When the JVM makes are call to the main method there is not object existing for the class being called therefore it has to have static method to allow invocation from class.

void - Java is platform independant language therefore if it will return some value then the value may mean different to different platforms so unlike C it can not assume a behavior of returning value to the operating system.

Read more about Java Fundamental Questions : Java Fundamental Questions


Web Development Tips and Tutorials - By Sachin
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

I would advise to SearchFirst, but in this case the search words form a bit of a problem: every post with code with a main method will turn up in the results.

Still, this question has been asked before, so perhaps if you can find the right words to sort on you might get a result with the answer.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why main() in java is declared as public static void main?