aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why is main() public? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why is main() public?" Watch "Why is main() public?" New topic
Author

Why is main() public?

Mohit Jain
Ranch Hand

Joined: Jun 04, 2007
Posts: 74
Hi Ranchers

A question often comes to my mind - "Why is main() method public?"
We may say that main() must be accessible to JVM as JVM calls main() directly at runtime that is why it should be public.

Lets have a look at following code -

class X
{
public static void main(String[] s)
{
}
}

Here the class is not public but main() is public. And the code works fine.

My question is -

If the class is not public (thus may not be accessible directly by JVM) then what is the point in declaring main() as public.
(If class is not accessible its method also wont be accessible).

Please explain and correct me where I m wrong.


SCJP 5.0, SCWCD in progress
Mohit Jain
Ranch Hand

Joined: Jun 04, 2007
Posts: 74
Can anybody please provide an explanation for this??
sapana jain
Ranch Hand

Joined: Oct 24, 2007
Posts: 42
Originally posted by Mohit Jain:
Hi Ranchers

A question often comes to my mind - "Why is main() method public?"
We may say that main() must be accessible to JVM as JVM calls main() directly at runtime that is why it should be public.

Lets have a look at following code -

class X
{
public static void main(String[] s)
{
}
}

Here the class is not public but main() is public. And the code works fine.

My question is -

If the class is not public (thus may not be accessible directly by JVM) then what is the point in declaring main() as public.
(If class is not accessible its method also wont be accessible).

Please explain and correct me where I m wrong.


hello ,
till now how much i have known java on that basic i can say that it is related to the concept of packages.if class is not public then it is default ok.
now in your case you have not made class public but main() is public because public is accessible every where while default scope is upto the current package only.
do tell me if you are satisfied with my answer.
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Hi Mohit,

Good question, but unfortunately there really isn't a deeper answer other than "because the Java language specification says so". The main method needs to be static so that it can be called without instantiating its containing class, but you're right in saying that there's no intrinsic technical reason why it can't have a different visibility status. In fact, I believe some early versions of Sun's JVM had no problems executing a Java application with a non-public main method (despite what the spec officially said).


SCJP 5.0
Mohit Jain
Ranch Hand

Joined: Jun 04, 2007
Posts: 74
Thanks Sapna and Kelvin for your replies.

I understand that if class is default then its not visible outside current package. At the same time any public methods inside the class will also not be visible in other packages as the class itself is not visible.

This at times make me think that how come JVM can directly call main() bypassing the class accessibility.

Friends if you get a better understanding of this then please let me know.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42286
    
  64
One way to look at it is that if a method isn't public, then it can only be called from a class in the same package, or an extending class, or the class itself, depending on which visibility is chosen. Since none of these 3 cases apply to starting an application from the command line, public is used (which means the method can be called from everywhere). Admittedly, this is a bit of hand-waving.


I think this sentence bears a bit of elaboration. The JVM can access all classes all the time; it is after all responsible for enforcing visibility rules, and thus perfectly capable of ignoring them if need be (e.g. if the user asks it to start an application). Only other classes have to play by the visibility rules.

But I think Kelvin comes close where he says "because the Language Spec says so". The normal Java rules don't really apply at this border to the operating system, so someone has to define how it works. Thats someone is the language spec :-)


Ping & DNS - my free Android networking tools app
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Why is main() public?