Yes, we can have multiple main()'s in a java application.And we can call a main() of one class to main()of another class.Here is the Eg: class Test{ public static void main(String[] args)throws Exception{ for(int i=0;i<args.length;i++){ System.out.println(s); } } class Arr{ public static void main(String[] args)throws Exception{ String s[]={"Xyz","lkm","abc"}; Test.main(s); } }
Above answer was rated as good by the following members: midhagaurav
RE: can we declare multiple main() methods in multiple...
Yaa u can declare multiple main methods in different classes this wont through any error while complition but it through u runtime exception stating that no main method found.So there should be only one main method in a program
RE: can we declare multiple main() methods in multiple...
No. We can not declare multiple main() methods as main() method is associated with the static keyword. Whenever the compilation starts compiler looks for the keyword static for compiling the program as static keyword associates with the main() method. This is the reason we need not pass any object to call the main() function. If there will be multiple main() methods then it would be difficult for the compiler to distinguish between the functions i.e to start the compilation from which main() function should be compiled first.
RE: can we declare multiple main() methods in multiple...
In a single application user can create multiple main methods in multiple classes. But while running the application user needs to give one class name (which is the enty point of the application) which must have to have a main method which will create the main thread to run your application.
RE: can we declare multiple main() methods in multiple...
yes we can declare. but we can run only one at a time. whatever method we want to execute we hv to give command as c:/java <name> no matter of whwich name our file is saved
RE: can we declare multiple main() methods in multiple...
Yes we can declare multiple main method in various classes. When you compile it creates many class files. Run any class that has main method it will execute only that class. It works my friend
RE: can we declare multiple main() methods in multiple...
Yes we can have multiple main()'s in a java application.And we can call a main() of one class to main()of another class.Here is the Eg: class Test{ public static void main(String[] args)throws Exception{ for(int i 0;i<args.length;i++){ System.out.println(s); } } class Arr{ public static void main(String[] args)throws Exception{ String s[] {"Xyz" "lkm" "abc"}; Test.main(s); } }