• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling main() of one class from another  RSS feed

 
Twinkle Kaur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have learn that every function in java is treated as a normal function including main() except that execution of a program starts here.
I tried to overload it.
But I am getting error while doing so via String type array as an argument of main.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither Hello1 nor Hello12 have a no-argument main method.
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main method does not take a String but an array of Strings. If you pass one String to the ... operator, it will be converted to an array.
 
Twinkle Kaur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry that I mixed two questions going on in my mind. What I want to know is that when I am calling main() of hello12 class in the main() method of Hello class why I am getting an error.
while there is no error in case of calling main() of Hello1.
It is right that when we pass one String to the ... operator, it will be converted to an array.
But how to deal with String[] ?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Twinkle Kaur wrote:What I want to know is that when I am calling main() of hello12 class in the main() method of Hello class why I am getting an error.

Because the main() method in class Hello12 expects a String[] (array of String objects) and you are trying to pass it a single String.

Twinkle Kaur wrote:It is right that when we pass one String to the ... operator, it will be converted to an array.

Yes, varargs.

Twinkle Kaur wrote:But how to deal with String[] ?

Pass it a String[] instead of a String.


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Twinkle Kaur wrote:It is right that when we pass one String to the ... operator, it will be converted to an array.
But how to deal with String[] ?

TBH, I think you're worrying a bit too much about the mechanics of the two styles. The fact is that they are almost equivalent; in fact, "varargs" is a superset of an array argument. That is to say: a varargs method will accept ANY argument that was successfully passed to a method that takes an array, but it will also allow a call passing 0 (very important), 1 or more individual arguments.

For full information, you need to read the JLS, which is rather dry; but essentially, I think what you say is true: it converts any list of individual arguments (including none) to an array. What I'm not exactly sure is what happens if you pass null. I think it converts it to a 1-element array containing a null, but I'm not certain.

When I need to know, you can be sure I'll check the JLS though.

My advice: For a main() method, use String...; otherwise, use varargs where it's useful. But DON'T mix the two, and don't overload varargs methods - which is easier to do than you might think.

Winston

PS: One thing to know when you're checking the JLS: It does NOT use the term "varargs". It refers to "variable arity" methods.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!