Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

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

user-image
Question added by ahmad yassine , Business Development & Performance , Boubyan Bank
Date Posted: 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
by 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

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
by 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

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.