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

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

متابعة

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

user-image
تم إضافة السؤال من قبل ghj ghjh
تاريخ النشر: 2016/08/30
Omar Boukhobza
من قبل Omar Boukhobza , Full-Stack Developer Consultant , CGI

The main() method is mandatory for the class's or application's execution. You may though define multiple main() methods in classes of the same package with no problems.

Ancy James
من قبل Ancy James , Admin assistant , Passion Wedding Parties Organizing

  For a java program only one main() method is required that should be in the source class. that method can be called by any classes defined in the program without object .the main method is independent of objects.

for  ex:

 

class Test()

{

public static void main(String args[])

{

Add ob= new Add();

System.out.println("Result =" +ob.Sum());

}

}

public class Add

{

int a= 2,b= 3,c=0;

public int Sum()

c=a+b;

return c;

}

}

here Test is the source class that requires main() method remaining classes not required that main() method. when execution starts source class will call other classes.

Ahmed Mostfa
من قبل Ahmed Mostfa , MSc. , VCU

There is only one Main method you should call to perform your program. Other classes may performed in a calling function inside Main(). 

 

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