Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

Can we have more than one main method in a class?

user-image
Question ajoutée par mahesh sharma , Software Developer , Tech Mahindra
Date de publication: 2013/11/30
Anas Rabei Abd ALLAH
par Anas Rabei Abd ALLAH , Senior Software Developer , The Center Of Strategic Studies

The answer is depending on what you want. 

 

If you want more than one main method with the same signature, the answer will not NO and this is not for the main method only, it is the same for any other method.

 

If you want just the name of the method, the answer will be YES. Simply you can overload the main method as nothing special to it.

 

The only fact that you should put in mind is that when the compiler start to run your application, it will search for the method with the following signature (public static void main(String[])) in the specified startup class.

yes, u can have more then one main method in a class just maintion the main method public which u  want to start the jvm execution.

Utilisateur supprimé
par Utilisateur supprimé

we can have have multiple main methods, but in that one main method must be PSVMain(String args[]) and others different from this.

Cedric Nabaa
par Cedric Nabaa , Java developer , Capital Banking Solutions

You cannot have morethan one main method in a class. The main method is like any other methods, You cannot have in java the same method (same signature: return type, method name, number/type of parameters) declared twice in a class

Yes,you can have more classes that contain the main() method, but at least one class which should contain main() and it should be public so that JVM will start that class as Main thread during compilation

Utilisateur supprimé
par Utilisateur supprimé

no.more than one main method can't be used in class of same .

NO THERE MUST BE A SINGLE MAIN METHOD IN YOUR CLASS..MAIN() IS ENTRY POINT OF OUR PROGRAM.MAIN FUNCTION CAN BE EITHER VALUE RETURNING OR NON VALUE RETURNING.

Preetham D
par Preetham D , Software Engineer / Senior Software Engineer , Mindtree

Class can contain may functions, but you can have only one main function

Utilisateur supprimé
par Utilisateur supprimé

only one main method

yes but the main with array of string is considered as main thread so this cant be override and apart from that we can have any no. of main methods

Ajith Fernandez
par Ajith Fernandez , Software Engineer , Nayasoft Solutions Pvt.ltd

No the program fails to compile. The compiler says that the main method is already defined in the class.

More Questions Like This

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?