doubt in public static void main

T

Thomas Hawtin

sarathy said:
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
 
S

sarathy

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
 
R

Robert Klemme

sarathy said:
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
 
M

Matt Humphrey

sarathy said:
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 (e-mail address removed) http://www.iviz.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top