Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overloading main()

 
vijayk kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can we overload a main() method?
When the compiler issues an error message " two entry points defined?"
 
Vijay Kumar
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you can overload main()
e.g 1) public static void main(String[] args,int a){
// some code
}
2)public static void main(String[] args){//JVM will call this method to start
// some code
}

 
Matt Harrah
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But to be clear --
although you can overload main, only the version with the standard signature will be executable as an application from the command line
 
vijayk kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I am now clear!
 
Peter Chase
Ranch Hand
Posts: 1970
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main() method is much less "magic" than beginners often imagine. It is just a public static method of a class, like any other. All the things that apply to any other public static method also apply to main().

The only thing that is special about main() is that the standard Java launcher calls it when starting an application.

The standard Java launcher will only call a public static method with the name "main" and accepting a String array. Other methods called "main" are allowed, but the standard Java launcher will ignore them. That said, it is generally bad practice to name any method "main" unless it is to be used as the application entry point.

I keep referring to the "standard Java launcher" because there are in fact many ways of starting Java code. For instance, applets are not started via main(), but have a life-cycle involving methods called init(), start(), stop() and destroy(). Also, you can write your own launcher in native code (e.g. C++), to start Java code in any way you like; this involves Java Native Interface and Java Invocation Interface.
[ August 14, 2006: Message edited by: Peter Chase ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vijayk kumar:
When the compiler issues an error message " two entry points defined?"

Never - that's not an error message that the Java compiler nor the Java launcher ever issues.
 
Pawankumar Jajara
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is possible to overload the main function in java. But the trick is that JVM will look for public static void main(String args[]) to load the class firstly. Once it finds the main with this signature, you can have other main functions being called within the same class. Make sure you change the input parameters to the main while overloading as the return type change wont help.

The working example will look like this:



Thanks,
Pawankumar Jajara
 
Campbell Ritchie
Sheriff
Posts: 48645
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please always indent your code correctly (spaces not tabs) and use the code button; since you are new I have edited your code and you can see how much better it looks.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Harrah wrote:But to be clear --
although you can overload main, only the version with the standard signature will be executable as an application from the command line


so, is overloading of main() not useful for any purpose and is just a trick ?
 
Campbell Ritchie
Sheriff
Posts: 48645
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is probably only a trick.
 
Pawankumar Jajara
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

Please always indent your code correctly (spaces not tabs) and use the code button; since you are new I have edited your code and you can see how much better it looks.



Hi Campbell,
Thanks for updating my code. I didn't knew it but will be careful next time onwards.

Thanks,
Pawankumar Jajara
 
Campbell Ritchie
Sheriff
Posts: 48645
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
kumarjit banerjee
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijayk kumar wrote:Hi All,

Can we overload a main() method?
When the compiler issues an error message " two entry points defined?"


Only two types of signatures are allowed in java

public static void main(String[] args){
}

public static void main(String... args){
}

Placing both types of signatures in the same class will result in compile error for main method only. For other methods the code will compile.
 
Paul Clapham
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Sudip Bose wrote:so, is overloading of main() not useful for any purpose and is just a trick ?


Let's put it this way: I don't have any use for it. And apparently you don't either. But there are several million users of Java. I'm not prepared to say that absolutely none of those millions of users have any use for it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic