Can a main() method of class be invoked in another class

Showing Answers 1 - 29 of 29 Answers

ravish

  • Jun 29th, 2005
 

yes,if the main() is declared as public.

Panchal Rajendra S

  • Jul 7th, 2005
 

yes, because it need not be instantiated.

  Was this answer useful?  Yes

Narasimha Chary

  • Aug 3rd, 2005
 

Yes it is possible

Sandip Gupta

  • Aug 3rd, 2005
 

yes, if that is not inistantiated.

raghu

  • Aug 24th, 2005
 

yes main is allso method we can call them in any class only within the static context 

  Was this answer useful?  Yes

sandeep patil

  • Sep 1st, 2005
 

Yes It can be

  Was this answer useful?  Yes

varadharajulu

  • Sep 11th, 2005
 

yes

  Was this answer useful?  Yes

D.S.Reddy

  • Sep 12th, 2005
 

Yes, its possible , but only if the class in which the main() is defined must be Public.

kmhariharan

  • Sep 13th, 2005
 

It is possible.we can easily invoke a main() of a class from another class.

I show  u demo here.

//Class Test

public class Test

{

public static void main(String args[]) //Main Method which is

//Invoked by another class Testing

{

System.out.println("Test Class main() Method is Called");

}

}

//Class Testing

public class Testing

{

public static void main(String args[])

{

if (args.length!=0)

{

Test.main(args);// Main method of Test Class is Calling

}

}

}

vikrant

  • Sep 26th, 2005
 

Can a main() method of class be invoked in another...

  Was this answer useful?  Yes

kranthi

  • Sep 26th, 2005
 

yes

  Was this answer useful?  Yes

Ramanath

  • Dec 12th, 2005
 

Yes, it is possible. but the example given above we have to make any one class as public and not both

  Was this answer useful?  Yes

Duke

  • Jan 18th, 2006
 

yes main method of a class can be invoked by any class by using the  static context

  Was this answer useful?  Yes

vaibhav

  • Jan 26th, 2006
 

for hariharan

there could only one class as public in a program,

and main can't be called by other class(by any class)

b'coz the main method is called by JAVA RUN TIME ONLY

  Was this answer useful?  Yes

krishna

  • Nov 14th, 2006
 

No it won't work.Only one main methods is allowed

  Was this answer useful?  Yes

ahmed ali

  • Jul 26th, 2010
 

Yes. You can invoke.

Here is code:

class A
{
public static void main(String args[])

System.out.println("Hi friend");

}

class B
{
public static void main(String args[])

A.main(new String[10]);
System.out.println(" How are you");
}
}

Save it as B.java and execute you get 

Output : 
Hi friend
How are you


  Was this answer useful?  Yes

yes .. I'll show you how it is done.

class A
{
        public static void main(String []args)
            {
                System.out.println("This is Main() of Class A");
             }
}

Write another Class name B
class B
{
        public static void main(String []args)
            {
                String s="this is use less";//variable which is not useful in this program.
                  A.main(s);//invoking main() method of class A. since main() method is   static it can be invoked by using Class name as a reference.
                System.out.println("This is Main() of Class B");
             }
}

Now compile both classes ..

LAXMINARSAIAH RAGIRI

  • Dec 27th, 2011
 

Yes, it is possible for these you should declare it as public.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions