• Post Reply Bookmark Topic Watch Topic
  • New Topic

Where is the main method?  RSS feed

 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We define a main(String a[]) in a java class.
This main method accepts a string array i,e it has a signature that it carries.
The jvm knows where to find the main method.

Where is this main method defined in Java?
Or is it just the method that jvm knows it has to call that has a particular signature?

It is just a question and feel free to answer.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sudipto Shekhar:
Where is this main method defined in Java?

Unlike C and its derivatives Java does not have a single entry point. Each class can have a main method.

Or is it just the method that jvm knows it has to call that has a particular signature?

That's just it. When you provide a class to run, the JVM loads that class, finds the "public static void main(String[])" method and executes it. I'll refrain from telling how it finds this method; all you need to know is that it must have this exact same signature.

Well, except it can throw any type of exception without a problem. So you can add "throws RuntimeException", "throws IOException", or even "throws Exception" and "throws Throwable". The following are all valid:

public static void main(String[] args)
public static void main(String[] args) throws IOException
public static void main(String[] args) throws IOException, RuntimeException
public static void main(String[] idontcarewhatmynameis)
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are other headings which the JVM will accept for the main method, but please stick to the conventional format. Remember you may have to declare the parameter final.
 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so it was not a foolish question indeed.
The main method can throw exception
The jvm knows that the class which it runs/loads will have a public static void main(String a[]) which
it will invoke only if the desired signature is matched.
Thank you Rob, Ritchie for the expert knowledge you shared with me.


 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sudipto Shekhar:
Ok so it was not a foolish question indeed.
. . .
The jvm knows that the class which it runs/loads will have a public static void main(String a[]) . . .


You are on beginner's: no question too small or simple.
The JVM "hopes" that the class which it runs/loads with have a public static void main(String args[]) method. If it can't find one, . . . well you can try it and see what happens.
 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the boost.
I like you.



[ December 12, 2008: Message edited by: Sudipto Shekhar ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!