English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

For example, I created 2 classes "Buy" and "Sell" which have the "main" method. Now I created a new class called "menu" which will prompt the user to enter their choice of what program to run. I designed it so that when user enter 1, it will run the "Buy", when user enter 2, it will run the "sell". But this is the problem. How to call the "main" method in the Buy and Sell class?

2007-01-18 12:41:37 · 4 answers · asked by sylvdoanx 2 in Computers & Internet Programming & Design

4 answers

The methods in the other classes must be public. If they are static as well, you can simply do ClassName.MethodName(). If not, you need to instanciate the class and then invoke the method.

2007-01-18 12:53:35 · answer #1 · answered by Rex M 6 · 1 0

It's a bit unusual that you would have a main method in all of your classes, but who am I to question your design. If all of the classes declare a main like the following:

public static void main( String[] args )

then your Buy method would be implement as follows:

public class Menu
{
public static void main( String[] args )
{
// Get the prompt from somewhere
String prompt = ...;

if( prompt.equals( "1" ) )
{
Buy.main( args );
}
else
{
Sell.main( args );
}
}
}

2007-01-18 14:39:36 · answer #2 · answered by Peter M 2 · 0 1

generally if you should only have one "main" method, such as the one in menu, and the other classes should either have static methods (add static before the type of the method "public static int getX()") that are invoked with ClassName.MethodName(), or the classes are made into objects with public methods that the menu class can access.

2007-01-18 12:52:59 · answer #3 · answered by Anonymous · 0 1

you should _read_ the compiler output, it probably would have told you that you are missing a return statement in getMax.

2016-05-24 05:17:34 · answer #4 · answered by Anonymous · 0 0

fedest.com, questions and answers