Mike Schilling wrote:
zerg wrote:
Mike Schilling wrote:
Kislay wrote:
The main function of java is public static void main() . Why is
it
public and why is it static ?
It's public because it's called from code outside its own class
(the JVM startup logic.)
Funny that serialVersionUID doesn't need to be public then. :-)
Or readResolve() etc. Serialization plays a lot of games.
Indeed. The point I was making being that when the VM itself is
reflectively doing stuff to the code, it isn't a technical
requirement
for anything to be public. The requirement of "main" to be public is
essentially gratuitous. But it does exist, and it seems to do no
harm;
arguably it breaks encapsulation of the main class, but generally
the
only possible clients of that class are by the same programmer
anyway,
and it's not normal to explicitly invoke a static method named
"main"
that looks like an application entry point anyway.
If it were an instance method, it would need an object to be
its "this", and thus would have to run after that object's
constructor.
Static initializers can construct objects and even invoke methods
on
them.
Yeah, but those are coded explicitly. If main() were an instance
method, what constuctor would you implicitly use? (Well, the
default one, obviously, but insisting that it exist and do the
right
thing is pretty inflexible compared to using a static method that
can do anything you want.)
True enough. I'm just pointing out that it IS possible for a
constructor to run before main(). Even if REQUIRING it wouldn't have
been desirable.
rather than a necessity, we agree completely. Note that there's
clearly Java-ish code that runs before main() is called. At the very
The system ClassLoader is created, as is a user ClassLoader.