• 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

main method must be public . Why ?

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to understand why main method must have to be public .
one reason I read is that because it is the entry point and invoked automatically from outside as a result of execution of the source file .

Now it that is the case why the class in which the main method is defined
does not have to be public ?

I am trying to simply understand the concept behind why main method must be public but the class in which main method is defined does not have to be public since it seems to me as contradicting ...

Please help me out
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're overthinking it. Ultimately, these rules were chosen for convenience of the implementor (i.e., the folks at Sun who write JVMs.) There are really no compelling, logical reasons -- it's just what made things easiest for them.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think some of the rules of how default access works were chosen to make it easier for beginners to run programs without necessarily understanding how packages and access modifiers work. Other rules were made without this consideration. So some rules are a bit inconsistent or weird - this is one of them. One other thing to realize is that many decisions were made early in the history of Java when there was a lot of excitement a nd pressure to get a release out the door quickly. Those decisions weren't always erfectly logical and consistent. But as long as the result is not too badly broken, Sun is very reluctant to change such behavior now, as it might break programs for people who are obeying the rules of the language as originally stated. So there are various bits of weirdness like this that we just live with now.
 
Chris Wox
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the reply folks
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So that main can be accessed from anywhere in the program and not only from the block where it is declared.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


one reason I read is that because it is the entry point and invoked automatically from outside as a result of execution of the source file .



Remember, we cannot have a private class. Actually, java syntax allows the public static void main(String[] a) to reside only in the class that is defined as public modifier or no modifier and not private modifier. (note: no modifier is different from private modifier)

note that main() is static, so that, even if we create multiple instances of the class that contains main(), we are going to have only on main() common to all (that is, we can access it without instance).

since main() is being an entry point and follows template design pattern, in most of the cases it is where you are going to instantiate other classes and you are not going to instantiate the class containing the main method from other classes.

So, i think java developers had this flexibility in mind while developing it.


Thanigaivel S.
[ June 15, 2006: Message edited by: S Thanigaivel ]
reply
    Bookmark Topic Watch Topic
  • New Topic