Communiquez avec les autres et partagez vos connaissances professionnelles

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

Suivre

Should a main() method be compulsorily declared in all java classes?

user-image
Question ajoutée par ahmad yassine , Business Development & Performance , Boubyan Bank
Date de publication: 2013/12/18

Yes we need a main method ,without main() the execution of program doesn't start and sometime we indirectly uses main() which is not available to our source code but its still  there e.g in J2EE technologies like jsp servlets we uses container or server wchich automatically uses main for java source code if not available,

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

Not in all java classes for sure. And if you are running a web project you dont even need a main to execute your java code. 

Not mandatory in all classes.but there should be a class with atleast1 main method.

cz without main method u can't execute  class file, you'll get an exception  as main method missing

Not necessary you need to define main() method in all java classes. for example java API classes doesn't have main() at all.

You can create a class withoout main() can be used by its instance in another class

Utilisateur supprimé
par Utilisateur supprimé

In jsp servlets we use container or server which automatically uses main for java source code. However in java application development we need atleast one main method of the parent class (and not to its child class's) to execute the program. 

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

You can have many .class file, but main() indicates the starting point of the program. So you should have one main() class(cannot have more than one)

More Questions Like This