• 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

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello every body,
Q. Can we overload main method in a class with different arguments like -
1. public static void main(String[] args)
2. public static void main(int[] args).

if yes then it is not necessary that a class must contains a single main() method.
thanx
kuldeep kalla
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, you can!
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As long as u have the main method with the mandatory signature
u can overload it in any way u like.
Otherwise the code might compile, but would give a noSuchMethodError at runtime.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you can overload the main(String args[]) method but the JVM is only going to call this method automatically and you have to yourself call all the overloaded methods from the main(String args[]). Hence there is no point in overloading the main().
This is because the JVM needs a starting point to start interpreting the bytecodes even before any objects are created, and hence it only looks for
public static void main(String args[])
method definition and starts the execution from this method.
If this method is not found it gives an exception stating no such method found.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic