Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to have two main methods in a java program ?

Reply
Thread Tools

How to have two main methods in a java program ?

 
 
aks_java
Guest
Posts: n/a
 
      06-10-2009
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 ?
 
Reply With Quote
 
 
 
 
Bent C Dalager
Guest
Posts: n/a
 
      06-10-2009
On 2009-06-10, aks_java <> wrote:
> 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
--
Bent Dalager - - http://www.pvv.org/~bcd
powered by emacs
 
Reply With Quote
 
 
 
 
aks_java
Guest
Posts: n/a
 
      06-10-2009
On Jun 10, 8:48*am, Bent C Dalager <b...@pvv.ntnu.no> wrote:
> On 2009-06-10, aks_java <atind...@gmail.com> wrote:
>
> > 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.
>


Thanks, now it works. Is this the same as overriding the main method ?
 
Reply With Quote
 
Bent C Dalager
Guest
Posts: n/a
 
      06-10-2009
On 2009-06-10, aks_java <> wrote:
> On Jun 10, 8:48*am, Bent C Dalager <b...@pvv.ntnu.no> wrote:
>> On 2009-06-10, aks_java <atind...@gmail.com> wrote:
>>
>> > 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.
>>

>
> 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
--
Bent Dalager - - http://www.pvv.org/~bcd
powered by emacs
 
Reply With Quote
 
Mayeul
Guest
Posts: n/a
 
      06-10-2009
aks_java wrote:
> On Jun 10, 8:48 am, Bent C Dalager <b...@pvv.ntnu.no> wrote:
>> On 2009-06-10, aks_java <atind...@gmail.com> wrote:
>>
>>> 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.
>>

>
> 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.)

--
Mayeul
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      06-10-2009
Mayeul wrote:
> 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.

--
Lew
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      06-10-2009

Mayeul wrote:

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



Sun's tutorial calls it "Hiding"
http://java.sun.com/docs/books/tutor.../override.html


--
RGB
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Is it possible to have two main functions in a c program? mike-yue C Programming 20 04-06-2008 04:36 AM
How to javac a java program w/ another java program which is w/o a main method cjeffwang@yahoo.com Java 1 10-31-2005 04:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57