Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Priyanka Dandekar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sachin Joshi
Ranch Hand
Posts: 83
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic