• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

static void main(String args[])

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
can anyone say why do main() need to be "public"?
when i tried as


it compiles good but, throws error as "Main method is not public" during run time.

-Thanks & Regards,
Hamsa
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can refer to this FAQ.
[ March 14, 2008: Message edited by: Christophe Verre ]
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, the short answer is: "Because the JLS says so."
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static void main(String[] args) {
}

Its a legal syntax for declaring a method called "main" with default access.
So there is no compilation problem.



If you want to run the "main" static method from command line, it needs to be declared public. Its defined by java spec.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hamsagayathri,

If you take the

It behaves like any method
with the name "main" which is "static" and the return type is "void" takes "String array" as argument.
So it will compile without any problem just like any other method.
But if you try to call any other method from this main method specially whih is out of the scope of the default modifier then it will throw exception as the modifier here is "default".

That is why it shld be public and can also be final.


Hope this help

Sandhi
 
Hamsagayathri Palanisamy
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sandhi mridul:
Hi Hamsagayathri,

If you take the

It behaves like any method
with the name "main" which is "static" and the return type is "void" takes "String array" as argument.
So it will compile without any problem just like any other method.
But if you try to call any other method from this main method specially whih is out of the scope of the default modifier then it will throw exception as the modifier here is "default".

That is why it shld be public and can also be final.


Hope this help

Sandhi



Thank you sandhi.What you said is exactly rite.
sandhi,I like to know what exactly happening when we run the program.
why the main() method is called first?
and also in the above reply you have mentioned like

"But if you try to call any other method from this main method specially whih is out of the scope of the default modifier then it will throw exception as the modifier here is "default"."

For sample i have tried following code.Imhavent call any method but, still it shows error while running program..



-Thanks & Regards,
Hamsa
 
Hamsagayathri Palanisamy
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sandhi mridul:
Hi Hamsagayathri,

If you take the

It behaves like any method
with the name "main" which is "static" and the return type is "void" takes "String array" as argument.
So it will compile without any problem just like any other method.
But if you try to call any other method from this main method specially whih is out of the scope of the default modifier then it will throw exception as the modifier here is "default".

That is why it shld be public and can also be final.


Hope this help

Sandhi



Thank you sandhi.What you said is exactly rite.
sandhi,I like to know what exactly happening when we run the program.
why the main() method is called first?
and also in the above reply you have mentioned like

"But if you try to call any other method from this main method specially whih is out of the scope of the default modifier then it will throw exception as the modifier here is "default"."

For sample i have tried following code.Imhavent call any method but, still it shows error while running program..



-Thanks & Regards,
Hamsa
 
Hamsagayathri Palanisamy
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sandhi mridul:
Hi Hamsagayathri,

If you take the

It behaves like any method
with the name "main" which is "static" and the return type is "void" takes "String array" as argument.
So it will compile without any problem just like any other method.
But if you try to call any other method from this main method specially whih is out of the scope of the default modifier then it will throw exception as the modifier here is "default".

That is why it shld be public and can also be final.


Hope this help

Sandhi



Thank you sandhi.What you said is exactly rite.
sandhi,I like to know what exactly happening when we run the program.
why the main() method is called first?
and also in the above reply you have mentioned like

"But if you try to call any other method from this main method specially whih is out of the scope of the default modifier then it will throw exception as the modifier here is "default"."

For sample i have tried following code.Imhavent call any method but, still it shows error while running program..



-Thanks & Regards,
Hamsa
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Hamsagayathri Palanisamy:
why the main() method is called first?


Again, because the Java Language Specification says so.

Originally posted by Hamsagayathri Palanisamy:
For sample i have tried following code.Imhavent call any method but, still it shows error while running program..


It throws an error because the main method is not public, and it is required to be public by the JLS. There is nothing more to it.
[ March 17, 2008: Message edited by: Jesper Young ]
 
Hamsagayathri Palanisamy
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sandhi mridul:
Hi Hamsagayathri,

If you take the

It behaves like any method
with the name "main" which is "static" and the return type is "void" takes "String array" as argument.
So it will compile without any problem just like any other method.
But if you try to call any other method from this main method specially whih is out of the scope of the default modifier then it will throw exception as the modifier here is "default".

That is why it shld be public and can also be final.


Hope this help

Sandhi



Thank you sandhi.What you said is exactly rite.
sandhi,I like to know what exactly happening when we run the program.
why the main() method is called first?
and also in the above reply you have mentioned like

"But if you try to call any other method from this main method specially whih is out of the scope of the default modifier then it will throw exception as the modifier here is "default"."

For sample i have tried following code.Imhavent call any method but, still it shows error while running program..



-Thanks & Regards,
Hamsa
 
Being a smart alec beats the alternative. This tiny ad knows what I'm talking about:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic