ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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

user-image
تم إضافة السؤال من قبل ahmad yassine , Business Development & Performance , Boubyan Bank
تاريخ النشر: 2013/12/18
Manish Gour
من قبل Manish Gour

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
من قبل 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

Jayakumar P
من قبل Jayakumar P

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
من قبل 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)

المزيد من الأسئلة المماثلة