• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why is main() public?

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody please provide an explanation for this??
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :-)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic