![]() |
|
|
|||||||
![]() |
Java - doubt in public static void main |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
sarathy wrote:
> I have doubt in the signature of the main method. > > Why public? > Any how only JVM is going to invoke it. No one is going to > call main from other classes/packages. Then a private will do for this. A method to call when starting a program seems like it should be very public to me. The only really odd thing is that the class does not need to be public. > Why static? > Why was the main method restricted from accessing > non-static instance variables/methods. Which object would you expect it to be called upon? > Why void? > Normally any main method MUST return a value so that it's > exit value is can be used by other programs. Why is it void here? Certainly in C++ the return can be elided. That's a special case in that language. For my money, that doesn't buy its way, but it falls out of growing up from K&R C. Java programs are multithreaded. Usually the main method exits almost immediately. It rather makes sense for the exit code to be specified when an exit is requested (through System.exit). Tom Hawtin Thomas Hawtin |
|
|
|
|
#2 |
|
Posts: n/a
|
Hi all,
I have doubt in the signature of the main method. Why public? Any how only JVM is going to invoke it. No one is going to call main from other classes/packages. Then a private will do for this. Why static? Why was the main method restricted from accessing non-static instance variables/methods. Why void? Normally any main method MUST return a value so that it's exit value is can be used by other programs. Why is it void here? Please clarify, Sarathy |
|
|
|
#3 |
|
Posts: n/a
|
sarathy wrote:
> Hi all, > I have doubt in the signature of the main method. > > Why public? > Any how only JVM is going to invoke it. No one is going to > call main from other classes/packages. Then a private will do for this. Why is no one going to invoke main other than the VM? You could well do that for testing purposes etc. And a public method has the added documentation advantage that everybody immediately sees that it's called from "outside". Maybe there's also some security manager issue. > Why static? > Why was the main method restricted from accessing > non-static instance variables/methods. Initially there is no object hence no instance to invoke the method on if it was non static. And how should the VM know which of several defined constructors it should use? > Why void? > Normally any main method MUST return a value so that it's > exit value is can be used by other programs. Why is it void here? I speculate it's because not all OS provide a numeric exit status for processes or handle it the same way. Regards robert |
|
|
|
#4 |
|
Posts: n/a
|
"sarathy" <> wrote in message news: ups.com... > Hi all, > I have doubt in the signature of the main method. > > Why public? > Any how only JVM is going to invoke it. No one is going to > call main from other classes/packages. Then a private will do for this. > Because main can be called by other applications as a means of launching it within an application rather than only being accessible by the JVM. > Why static? > Why was the main method restricted from accessing > non-static instance variables/methods. Because a non-static context would require the application to create an object solely for the purpose of launching the application and it would have to have both a particular constructor and launching method. This is reasonable for applets which already have a complex connection to their context but seems unnecessary for applications. A static context does not prevent an application using a specialized object for startup but also does not require it, so I find it simpler. > > Why void? > Normally any main method MUST return a value so that it's > exit value is can be used by other programs. Why is it void here? Because the completion of main does not necessarily denote the end of the program as there may be a GUI running or other service threads. The program isn't over until it says its over (either all the non-daemon threads complete or System.exit is called) at which point a System.exit () call will provide the return code. Matt Humphrey http://www.iviz.com/ |
|
|
|
#5 |
|
Posts: n/a
|
sarathy wrote:
> I have doubt in the signature of the main method. > Why public? > Why static? > Why void? Rather than repeat a previous post of mine on this topic, here's a link to Google's archive http://groups.google.co.uk/groups? -- chris |
|