• 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

main method without public modifier

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Program compiles and runs fine if public
modifier is omitted from the main method.
What are the possible problems if main
method is not declared public?
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try running your code from another directory?
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it runs fine and it is perfectly legal. It will run even when you declare the main method as private!!!
Apparently it was reported to Sun a long time ago and they dismissed it by saying it is not a bug and they are not going fix it.
But remember, for the sake of SCJP, only
public static void main(String[] whateverYouWantToCallIt )
is the correct signature. That is the gospel.
Ajith
[This message has been edited by Ajith Kallambella (edited August 17, 2000).]
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marcela Blei:
Try running your code from another directory?


It doesn't matter from where u run it. Logically, private static void main should NOT work. But like Ajit said, its a bug in Java 2
JDK 1.1 does give u a compile time error.
As per the JLS main should be public static void.
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Friends,
I had tried and got the following result -
main methods of works in
Public and default class: with the following main methods:
1) static void main(String args[])
2) public static void main(String args[])
3) private static void main(String args[])
4) protected static void main(String args[])
(I have checked with and without a package statement in the code and in other directory and same directory respectively.)
There is a compile time error in private & protected class.
Yeh!! I too join your forum... why main should be "public static void"???
This forum should take forward this and clarify all of us.
One more outcome on this research, class that containing main method can be inherited subject to that main method should not have String argument (String xxx[]). You can say super.main(); in inherited class.
Thanks and regards,
[This message has been edited by V Srinivasan (edited August 20, 2000).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic