Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we add two main method in a single class have same signature.

 
gauraw gupta
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can it possible to add two main method public static void main(String args[]) in a single class.
Also please provide me the some information that a Class and Object are a simple class or abstract class or interface.

Thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gauraw gupta wrote:Can it possible to add two main method public static void main(String args[]) in a single class.

Have you already tried? If yes, what were the results? And why would you get those results?
 
Madhan Sundararajan Devaki
Ranch Hand
Posts: 312
Java MS IE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot, as it will result in compilation error. The reason being JVM will not know which method to invoke.
 
Maneesh Godbole
Saloon Keeper
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also please provide me the some information that a Class and Object are a simple class or abstract class or interface.

Recommended reading: http://www.coderanch.com/how-to/java/InterfaceVsAbstractClass
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you ever have two methods with the exact same signature, regardless of the name, in a single class? Why would 'main' be any different?

if you did have it in there twice, how would the JVM decide which one to use when it starts?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic