• 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

Does the main method have to be public?

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the main method have to be public?
becoz...even private works fine.
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this link (Requires an account!)
It is a bug! But Sun has closed the problem saying "It won't be fixed" with the comment "The runtime allows call to private methods, because of reflection. Fixing it will cause potential troubles."
It violates the Encapsulation for sure!
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BUT put in mind that for the exam you have to go with the JLS not the compiler. JLS says it must be public.. so if it's private in the exam.. it should give an error at run-time..
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Explanation by Kathy Sierra (to appear in the new SCJP FAQ):


Question: Is it allowed to declare the main method private?
Answer: Provided by Kathy Sierra
Former JVM versions (pre-1.4) allowed the main method to have any accessibility (private, etc).
This incompatibility with Section 12.1.4 of the Java Language Specification has been fixed as of version 1.4.
In order to invoke a main method from the command-line, it is now mandatory to declare the main method as follows:
public static void main (String[] args)
If the main method has any access level other than public, it will no longer run from the command-line.

 
Poornachandran R
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to RHE book the main() method is declared public by convention.It can have any visibilty modiefier.However, it is requirrement that it be static and void.
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In order to invoke a main method from the command-line, it is now B]mandatory[/B] to declare the main method as follows:
public static void main (String[] args)


Just a note for those who doesn't know :
Becareful that there are many variations of declaring an array :

[ November 26, 2002: Message edited by: Ioow Gneb ]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Poornachandran R

A last initial does not match the JavaRanch Naming Policy. Please adjust your display name. You can change it here.

Thanks for your co-operation!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic