aspose file tools*
The moose likes Beginning Java and the fly likes Why cant i call main method from another class having main block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why cant i call main method from another class having main block" Watch "Why cant i call main method from another class having main block" New topic
Author

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

Nipun Devlekar
Greenhorn

Joined: Dec 07, 2005
Posts: 9
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

Joined: Aug 16, 2005
Posts: 14420
    
  23

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
There's another problem with your code, but you will discover that at runtime


There is no emoticon for what I am feeling!
Nipun Devlekar
Greenhorn

Joined: Dec 07, 2005
Posts: 9
thanks jeff
i found the code executed smoothly which was the other problem you were
saying??
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14420
    
  23

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

Joined: Oct 02, 2003
Posts: 11494
    
  16

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!!!


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Nipun Devlekar
Greenhorn

Joined: Dec 07, 2005
Posts: 9
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

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Oct 02, 2003
Posts: 11494
    
  16

Thanks for changing your name!!!
[ December 07, 2005: Message edited by: fred rosenberger ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14420
    
  23

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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why cant i call main method from another class having main block