• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why cant i call main method from another class having main block

 
Nipun Devlekar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is wrong in doing this
//LocalMain
public class LocalMain{

//fields
int a;


//mainline

public static void main (String[] args){

//fields
int new1=9;
float new2=8;


//instance
LocalMain good = new LocalMain();

//OUTPUT
System.out.println("the value for a="+ good.a );

System.out.println("the value for new1="+ new1 );
System.out.println("the value for new2="+ new2 );
System.out.println("the value for Local="+ good );
good.main("hello");

}
}


/*errors that were created while writing program

LocalMain.java:26: Incompatible type for method. Can't convert java.lang.String
to java.lang.String[].
good.main("hello");

*/
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hick hick, your name does not comply to the JavaRanch official policy on registered names. Please change your display name to your real name.

Your main method expects an array of strings and you're trying to call it with a single string. Try this:
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's another problem with your code, but you will discover that at runtime
 
Nipun Devlekar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks jeff
i found the code executed smoothly which was the other problem you were
saying??
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your entire program looks exactly as in your first post, it will stop after running for some time with either a StackOverflowError or an OutOfMemoryError, because there's an unbounded recursive call in it: you call the main() method from the main() method itself. It will keep calling itself over and over again until the stack overflows or you have created so many LocalMain objects that the memory is exhausted.

Just tried to compile and run your program: I got a StackOverflowError.
[ December 07, 2005: Message edited by: Jesper de Jong ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nipun,

As Jesper said, you name does not conform to our naming policy. It's one of the few rules we have around here, and we take it pretty seriously. Basically, you need a first name, a space, and a last name. No obviously ficticious names are allowed, either.

Please visit here to change yours.

We're happy you're here, and are delighted to answer your questions. Many of us came here for the help, and stayed for the community. Hope you do the same!!!
 
Nipun Devlekar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies
i did not get a stack error but a continuous running screen .
As i am using a unix OS i did not get error;may be stack error reports in DOS .
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nipun Devlekar:
Thanks for your replies
i did not get a stack error but a continuous running screen .


Is that what you referred to earlier as "the code executed smoothly"
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for changing your name!!!
[ December 07, 2005: Message edited by: fred rosenberger ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nipun Devlekar:
Thanks for your replies
i did not get a stack error but a continuous running screen .
As i am using a unix OS i did not get error;may be stack error reports in DOS .


You'll just have to wait a bit longer for the stack overflow...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic