How to have two main methods in a java program ?

A

aks_java

Hi, I've built a GUI calculator and now my professor is asking the
following:

Prove the separation by having two “main” programs for the
calculator, one that calls the GUI version of the calculator, one that
calls a command line version of the calculator. The command line
version should have an interface that shows the current display
between square brackets, then a prompt (like the “>” sign) then awaits
your input. So a sample session should look something like :

[0.] > 78

[78.] > -

[78.] > 5

[5.] > =

[73.] > quit

Now here's my problem: How do I've two main methods in a single java
package ? I've looked around the internet to find examples of this
but I could find none that actually worked. Netbeans gives errors
straight away. I tried this for example:

class Checkmain{
public static void main(String args[]){
args[1]="ashish";
System.out.println("hello ");
}
}
class Checkmain1 extends Checkmain{
public static void main(String args[]){
System.out.println("how r u");
}

}
class Jo{
public static void main(String args[]){
String S[]=new String[10] ;

Checkmain.main(S);
Checkmain1.main(S);
}}

Now, I wonder if it is even possible to do this. How come no java book
talks about it ?
 
B

Bent C Dalager

Now here's my problem: How do I've two main methods in a single java
package ?

Put them in separate files. In your example, put Checkmain in
Checkmain.java, Checkmain1 in Checkmain1.java and Jo in Jo.java.

Cheers,
Bent D
 
A

aks_java

Put them in separate files. In your example, put Checkmain in
Checkmain.java, Checkmain1 in Checkmain1.java and Jo in Jo.java.

Thanks, now it works. Is this the same as overriding the main method ?
 
B

Bent C Dalager

Thanks, now it works. Is this the same as overriding the main method ?

No, the main method is static so you can't override it.

When you run the program you need to choose which class gets to run
and it is the main method in this class that will be executed. If you
say you want to run the Checkmain class, then Checkmain's main() will
be run and if you say you want to run the Checkmain1 class, then
Checkmain1's main() will be run.

That Checkmain1 is derived from Checkmain is not significant in this
case since everything happens in a static context.

Cheers,
Bent D
 
M

Mayeul

aks_java said:
Thanks, now it works. Is this the same as overriding the main method ?

No, it's different. The main method being static, there is little point
in overriding it anyway.

(Actually, I'm not even sure defining a new main method in a subclass
would be called overriding.)
 
L

Lew

Mayeul said:
No, it's different. The main method being static, there is little point
in overriding it anyway.

(Actually, I'm not even sure defining a new main method in a subclass
would be called overriding.)

It isn't. It's called "hiding". JLS, s. 8.4.8.2.

Overriding is a very specific thing, allowing subclass (instance)
methods to be invoked through a supertype reference.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top