File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion about public static void main()

 
Mandar Puranik
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Alton Hernandez
Ranch Hand
Posts: 443
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 867
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Praful Thakare
Ranch Hand
Posts: 642
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess this will not work on jdk1.4...
cheers
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Francis Siu
Ranch Hand
Posts: 867
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 867
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic