• 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

Can we override or Overload Main mathod

 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

This is one of the interview question which was asked to me today.

Can we override or overload a main method...If yes how and if no the why not.


Thanks in Advance
 
Marshal
Posts: 28245
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, why not? There's nothing special about the word "main". Although remember that you can't override static methods.
 
Ranch Hand
Posts: 57
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you can. Here is the example. Hope you found the answer for your question.

 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Subhash

You are not overriding the main method in the code snippet you have provided. You are simply calling the main method in the normal static way. No override occurs as static methods belong to the class, not an instance of a class and hence, cannot be overridden.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can overload the main method...but the JVM will only ever use the one with a String[] argument as the entry point for the application.
 
deepak carter
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you let me know in more detail Matthew


regards
deepak
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I mean is that the following is perfectly valid code:

Here, main has been overloaded, just like any other method can be overloaded. When you run this class from the command line, you will only ever get "The main program is running" as output. The JVM will only look for a method with the exact signature public static void main(String[]). It will ignore any other main methods with a different signature.

However, it's perfectly possible to call the other method explicitly from elsewhere in the code, by calling TestMainOverload.main().

Does that help?

(I'd also add that, while you can do this, it doesn't mean that you should do this. In fact, you should avoid it - it's just likely to lead to confusion).
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And, as already mentioned, you can define a non-static method called main, and override it in a subclass:


But I wouldn't consider that as overriding the main method. When someone refers to the main method in a Java context, they're usually talking about public static void main(String[] args). That method can be overloaded, but, because it is static, it cannot be overridden.

So you can definitely overload the main method, and you can override a main method, but you can't override the main method (by the common usage of the phrase "the main method").

And of course, just because you can do something, it doesn't mean that you should do it. I've never run across a situation where it made sense to define main as anything other than public static void main(String[] args).
 
deepak carter
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You all.Its a Great Place
 
Yeah, but is it art? What do you think tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic