Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

simple java question

  Asked By: Rabiah    Date: Apr 24    Category: Java    Views: 852
  

i have a question , i want to know in a single java source code how many main()
could be there???

say

////

class x
{
--------
--------
-------
public staic void main()
{ ... }

}


class y
{
--------
--------
-------
public staic void main()
{ ... }

}


is this possible in java?? so what could be my file name x.java or y.java???

thanks..

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Nixie Schmidt     Answered On: Apr 24

Actually this the first time I try this out. But it works just fine.
Thinking it over, the reason is that a single  file of java  source code  can hold
any number of classes but with one condition. This condition is that ONLY ONE
class can be declared as public. And because there are no public class  in the
code you provided as an example, you can name this file  after any calss name in
this file.
That's number one.
Number two, you can write as many main  methods as you like with any signature
you want. but note that the only signature the JVM realy invokes is with the
signature:
public static void  main(String[] args) { }
So, if you provided a method like the following
static void main(String[] args)
The JVM will not invoke this method ecause it's not public. Also, a public main
method but not static will not be invoked. Even if you tried out the following
code, it will not work:
public static void Main(String[] args) { }
** note that the method name is Main not main. Java is case sensitive.

There is one last note you must take care of while you are applying different
classes, each with it's main method, in the same source  code file.
If you named the example you gave with the name (x.java), running the x.class
file will run the main method of the x class. if you tried running the y.class,
then you are invoking the main method of the y class, not x.

 
Answer #2    Answered By: Isabelle Brown     Answered On: Apr 24

From my point u can't write main() more than one. There must be one
main function in one class.

 
Didn't find what you were looking for? Find more on simple java question Or get search suggestion and latest updates.




Tagged: