File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is main() public?

 
Mohit Jain
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohit Jain
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody please provide an explanation for this??
 
sapana jain
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 513
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Mohit Jain
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic