File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Calling main() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling main() method" Watch "Calling main() method" New topic

Calling main() method

Chandra Mouli

Joined: Dec 22, 2003
Posts: 1
I wrote a program like the one below.
public class Test {
private static void main(String args[]) {
Instead of declaring main method as public, I declared it as private.
But still the program was running without any error and printed "Hello".
Can anyone please tell what is actually happening?
Thankx in advance...
Igor Ko
Ranch Hand

Joined: Jun 24, 2002
Posts: 90
Obviously it depends of java (JVM).
How it works - search in public class the main function, and
check its signature (type of argument, output value, static/not
static, public/private/protection/default)
Probably used version of JVM don't have check of access attribute,
or the check accepted not only public, but your case.
What is the problem?
Exists a lot of JVM.
And men writing JVM aren't alwais perfect.
Pratibha Malhotra
Ranch Hand

Joined: Dec 21, 2003
Posts: 199
Yes .. It does work perfectly OK...
As a matter of fact private main method issue is a known problem with a bug report opened against it.
Sun has however closed the bug report and the problem will NOT be fixed.
But be careful while taking the certification exam.

~ Pratibha Malhotra<br /> <br />Sun Certified Java Programmer<br />SCEA 1.4 (In Progress)<br />~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />"Many of life's failures are people who did not realize how close they were to success when they gave up!!"
Igor Ko
Ranch Hand

Joined: Jun 24, 2002
Posts: 90
OK, so Sun doesn't think that is important.
Sun Uncertified java programmer.
[ December 22, 2003: Message edited by: Igor Ko ]
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Note that this was an issue of older JVMs. Sun's latest 1.4.2 JVM requires that the main method be public.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Calling main() method
It's not a secret anymore!