Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - doubt in public static void main

 
Thread Tools Search this Thread
Old 08-02-2006, 10:12 AM   #1
Default Re: doubt in public static void main


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
  Reply With Quote
Old 08-02-2006, 03:03 PM   #2
sarathy
 
Posts: n/a
Default doubt in public static void main

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

  Reply With Quote
Old 08-02-2006, 03:08 PM   #3
Robert Klemme
 
Posts: n/a
Default Re: doubt in public static void main

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
  Reply With Quote
Old 08-02-2006, 03:18 PM   #4
Matt Humphrey
 
Posts: n/a
Default Re: doubt in public static void main


"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/


  Reply With Quote
Old 08-03-2006, 08:47 AM   #5
Chris Uppal
 
Posts: n/a
Default Re: doubt in public static void main

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


  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump