This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling main() method" Watch "Calling main() method" New topic
Author

Calling main() method

Chandra Mouli
Greenhorn

Joined: Dec 22, 2003
Posts: 1
Hi,
I wrote a program like the one below.
public class Test {
private static void main(String args[]) {
System.out.println("Hello");
}
}
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.
Igor
Sun Uncertified java programmer.
[ December 22, 2003: Message edited by: Igor Ko ]
Dirk Schreckmann
Sheriff

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: http://aspose.com/file-tools
 
subject: Calling main() method
 
Similar Threads
Main Method
problems running code
main method in base class
Doubt about "public" in main
WHy does main() work with private accessibility?