Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

doubt in public static void main

Thread view: 
sarathy - 02 Aug 2006 15:03 GMT
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
Thomas Hawtin - 02 Aug 2006 10:12 GMT
>             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
Robert Klemme - 02 Aug 2006 15:08 GMT
> 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
Matt Humphrey - 02 Aug 2006 15:18 GMT
> 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 matth@ivizNOSPAM.com http://www.iviz.com/
Chris Uppal - 03 Aug 2006 08:47 GMT
>             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?selm=ROqdnRatPZKwSA3dRVn-gg@nildram.net

   -- chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.