aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Does the main method have to be public? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Does the main method have to be public? " Watch "Does the main method have to be public? " New topic
Author

Does the main method have to be public?

Poornachandran R
Ranch Hand

Joined: Sep 11, 2002
Posts: 47
Does the main method have to be public?
becoz...even private works fine.
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
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!


Mani
Quaerendo Invenietis
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
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..


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Poornachandran R
Ranch Hand

Joined: Sep 11, 2002
Posts: 47
Thanks a lot
Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93
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.


SCJP1.4(91%)
Timothy Toe
Ranch Hand

Joined: Oct 19, 2002
Posts: 156
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 ]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
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!


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does the main method have to be public?