aspose file tools*
The moose likes Beginning Java and the fly likes overloading main() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "overloading main()" Watch "overloading main()" New topic
Author

overloading main()

vijayk kumar
Greenhorn

Joined: Jul 29, 2006
Posts: 18
Hi All,

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

Joined: Jul 24, 2003
Posts: 260
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

Joined: Aug 05, 2006
Posts: 54
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

Joined: Jul 29, 2006
Posts: 18
Thanks, I am now clear!
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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 ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14104
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Pawankumar Jajara
Greenhorn

Joined: Mar 29, 2011
Posts: 4
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

Joined: Oct 13, 2005
Posts: 38340
    
  23
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

Joined: Jan 21, 2011
Posts: 637

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 ?

SCJP 6. Learning more now.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
It is probably only a trick.
Pawankumar Jajara
Greenhorn

Joined: Mar 29, 2011
Posts: 4
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

Joined: Oct 13, 2005
Posts: 38340
    
  23
You're welcome
kumarjit banerjee
Ranch Hand

Joined: Mar 27, 2011
Posts: 32

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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overloading main()