Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private static void main(String args[]) ...!!!!!!

 
Devesh H Rao
Ranch Hand
Posts: 687
Hibernate jQuery Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ppl,
If we go by the defn of access modifiers whenever a method is denoted as private it means that it can be accessed from within a class only.
now what i did was i tried the private access modifier for PSVM(String args[]){} and it worked.
what i would like to know is how does the PSVM method fit into all this and where does the method lie exactly for it to work even with a private access modifier.
 
Alfred Kemety
Ranch Hand
Posts: 279
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the JLS it should not work with a private modifier, but still some compilers and JVM work fine if you use the private modifier, it shouldn't though...
Rule of the thumb, follow the JLS in the code you're writting so that you will have no problem with any compilor, JVM
 
Devesh H Rao
Ranch Hand
Posts: 687
Hibernate jQuery Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alfred,
According to the JLS it should not work with a private modifier

That's exactly what i thought that it should not work but it does and i kinda wanted to know why and how it does work even in a few compilers that it works on.
i am not using the access modifier in any of my code and i stick to the JLS but why private SVM(String args[]) works is something i feels depends on how the JVM is built and the ClassLoader invocations.
any light on this by anyone who knows why it works will be very much appreciated.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a hole in the implementation. It's always been like that and if they changed now it might cause problems to existing code.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, they did change it to conform to the previously-existing spec, as of 1.4. Try creating a private main method now - you get "main method not public". Older JDKs allowed it, but not the latest ones.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It still compiles but you get the error message when you attempt to run. This solution is worse than treating private main methods as public.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic