• 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 question

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is this also a volid main method declaration, why? please explain. Thanks
final static public void main (String args[]){};
 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dian,
Yes it is!
A main method can be declared final private, public,protected or friendly(default). If any method is declared final then the method can not be overridden in it's subclass. See the two codes given below (filemname MainTest.java):
code 1 : run it with java MainTest

code 2 : run it with either java MainTest or java Super

Hope it will help you!

------------------
azaman
 
Dian He
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ashik,
Thanks for the explain, it does work that way, but I have come across a lot of place said the main method must have exactly the same signature, can you explain more.
 
Ashik Uzzaman
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dian,
main() should always be declared public as u want ur programm run in any platform any time. So there are some runtime environments who will seek main() and if private may not access it. I don't know which environments can't find out a private main(). May be someone else will explain better....

------------------
azaman
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aashik,
When I execute first code,the code compiles but when i run it (java MainTest) , it tells me that main must be public and static.
Is that the correct output ?
Thanx
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic