File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is private allowed as modifier for main?

 
Chandra Bairi
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is private allowed as modifier for main? if so then why is it recommended that we have the following signature for main
public static void main(String[] s)
Why is private allowed? I have read in a site that to make the JVM tolerant. what does that mean?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing special about the name "main." You can have functions named

or whatever you want.
But, by convention, the "java" command-line tool looks for a method declared as "public static void main(String[])" to invoke when you pass it a class name. If the signature is different, it may, or may not, work. It's just a convention, not a strict rule.
 
Ransika deSilva
Ranch Hand
Posts: 524
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, by convention, the "java" command-line tool looks for a method declared as "public static void main(String[])" to invoke when you pass it a class name. If the signature is different, it may, or may not, work. It's just a convention, not a strict rule.
it may
Could you please explain this. How can it get executed even the main method signature is not the conventional one(public static void main(String arg[]).
Thank you.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The interpreter could still execute it if it were protected, default-protected, or private; there's nothing to stop an implementation of the Java command-line executable from calling java.lang.reflect.Method.setAccessible(true) on the Method object that represents a private version of main(). But it's not guaranteed to work.
There's probably no implementation of Java in existence that would still launch main() if the return type or argument types were different, but again, that's just by convention; there's nothing stopping me from writing a version that found any method named main(), and called it regardless of its signature. But changing the protection was all that the original poster was asking about.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic