wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusion about public static void main() 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 "Confusion about public static void main()" Watch "Confusion about public static void main()" New topic
Author

Confusion about public static void main()

Mandar Puranik
Ranch Hand

Joined: Jan 08, 2003
Posts: 35
Hi,
Many a times in this forum this topic has been discussed. Still I am getting confused by the following :
Many guys said here that the entry point to a java program , there has to be a public static void main(String[]args) method. (According to JLS)
But I have following code, and its gets executed.
Whats the reason ?
Note : If we remove the static word, then it obviously doesnt run. But why does it run even if the access specifier is private or proteced ?

Thanks in advance.
Mandar


To Bug is Human,<br />To Debug Divine... :-))
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Mandar Puranik:
Hi,
Many a times in this forum this topic has been discussed. Still I am getting confused by the following :
Many guys said here that the entry point to a java program , there has to be a public static void main(String[]args) method. (According to JLS)
But I have following code, and its gets executed.
Whats the reason ?
Note : If we remove the static word, then it obviously doesnt run. But why does it run even if the access specifier is private or proteced ?

Thanks in advance.
Mandar

$ java TestDemo
Main method not public.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Mandar
Actually,in main method can contain any type access modifier such as private,public,protected or package.
The compiler would not give you a compiler error
Because the main method is a part of the application programme and other class can invoke main method directly.You can test the following code

You will get the funny result
Try it
Do not click the ctr-C or ctr-Z whenever using UNIX or other system
[ June 06, 2003: Message edited by: siu chung man ]

Francis Siu
SCJP, MCDBA
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 631
I guess this will not work on jdk1.4...
cheers


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
According to Bug Parade, this was fixed in merlin-beta2. (merlin is the code name for 1.4)
[ June 06, 2003: Message edited by: Thomas Paul ]

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Thomas
According to Bug Parade, this was fixed in merlin-beta2. (merlin is the code name for 1.4)
What is that means?
I guess this will not work on jdk1.4...
Did you try it?
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
This means prior to version 1.4 it will work but with 1.4 and probably with versions after 1.4 it will not work. Earlier when I was studying for SCJP this used to bug me a lot. Because in the study guide it was written it won'ytwork but when you actually do it, it works.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
This means prior to version 1.4 it will work but with 1.4 and probably with versions after 1.4 it will not work.
You mean it work before jdk1.4 (version<jdk1.4)
And it would not work >=jdk1.4
You mean that I am OUTDAY
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion about public static void main()