• 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 a class has two main's

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If So then which one will be triggered?? I am pasting the piece of code for reference.
 
Narayana.Vaddi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I didn't pasted the code correctly,this is the correct code!!
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

well, you can have two methods "main" with the condition that the list of arguments is different for the two. This is the the principal rule for overloading a method (this is what you are trying to do). This applys not only to "main" method, but to any other method made in Java.
So, the code in your second post will not compile, because the list of parameter is the same (String[] args), but the code in the first post will compile.
At runtime the method "main" with (String[] args) will be automatically called.
 
Narayana.Vaddi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried running the code pasted first but according to you i have to get the output as Main Method1 and Main Method2, but i am getting the Main Method1 only, how come it can be,as it is not taking any arguments or is there any other reason
 
camelia codarcea
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I never said you should get both "Main Method1" and "Main Method2"!!!
What I said is that the first post is correct, it will compile, but only the first method will be automatically called, because it has the "String[] args" as the list of parameters.

The second method will not be called (it is not explicitly called from the first method), so you cant get also the result "Main Method2".

If you want to get also the "Mai Method2", you should change your code as following:

public static void main(String[] args) {
System.out.println("Main Method1");
main("somethig"); // this calls the overloaded method "main" !!!
}
public static void main(String args){
System.out.println("Main Method2");
}
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Narayana",
Please check your private messages.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A method's signature is its name combined with its argument list. So if you change the argument list, that's enough to change the signature.

A main method used as an entry point must be "public static void" with a String array as its only argument. You can overload main, but the overloaded versions can't be used as an entry point.

(Note: In this context, we're concerned only with the order and the types of the arguments. We don't care about the local variable names.)
 
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I was just watching this thread and I have a question. Why doesn't the below code work where as the commented part works?/

class TwoMainMethods
{
public static void main(String[] args)
{
System.out.println("Method 1!");
main(2); //main("2");
}

public static void main(int[] args) // String args
{
System.out.println("Method 2!");
}

}

Thanks.

[ February 09, 2008: Message edited by: Arjun Reddy ]
[ February 09, 2008: Message edited by: Arjun Reddy ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
main(2); //main("2");
}

public static void main(int[] args) // String args
{
 
Vikram Shinde
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
main(2); //main("2");
}

public static void main(int[] args) // String args
{


It should have main method with int as parameter not int[] array
try this-public static void main(int args)
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arjun Reddy:
...where as the commented part works? ...


I'm not sure I understand your question, because the commented part does not work.

You have a main method defined to take a String array, and another main method defined to take an int array. You're trying to call main using an int (2) or a String ("2"). Neither of these calls will work, because neither are using arrays.

On the other hand, if you define your main methods to take varargs, then these calls would work, because the int and String would be automatically put into arrays of size 1.

(Warning: If you use varargs with main(String... args) and call main("2") inside that method, then you will have infinite recursive calls.)
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
come to think of it: var-agrs won't work either



A var arg and an array is the same isn't it? they can't be overloaded anyway...interesting
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone.

Please always note that the "public static void main(String[] args){...}" method is, for understanding proposals, like any other public static method. So tou canot use the same signature to declare two methods in the same class.

tks.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ali Khalfan:
... A var arg and an array is the same isn't it? they can't be overloaded anyway...


That's correct. Varargs just puts the array instantiation behind the scenes.

What I meant by suggesting varargs is replacing the array argument with a vararg...

This way, you could call main with a single String -- for example, main("2"); -- and the String would be "packaged" into a String array containing a single element.
[ February 10, 2008: Message edited by: marc weber ]
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marc,

In short, these two below are the same method declarations. Is that correct?

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

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

But Java 5.0 suggests that the 1st one should be the standard signature, right?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This Java Tech Tips article talks about similarities and differences between varargs and arrays.
 
camelia codarcea
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ali,

you wrote:
come to think of it: var-agrs won't work either
code:
public static void main(String [] args){
System.out.println("original");
}

public static void main(String... x){
System.out.println("tst");
}


But, actualy, both work, becaue starting with java 1.5, there are accepted the following three signatures for the main method (page 766 K-B book)

static public void main(String[] args)
public static void main(String... x)
static public void main(String bang_a_gong[])

So, the important things are: the method should be static and public, should be called "main". The parameter can be either var-args, or an array of Strings.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by camelia codarcea:
...actualy, both work...


I think Ali's point is that these will not work together. In other words, either one works by itself, but this is not a valid way to overload a method.

The reason is that varargs is just a shorthand way of using an array as a parameter. So the three examples you gave are not really different signatures -- they are just different ways of writing it.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic