• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is private allowed as modifier for main?

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic