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 ghj ghjh
Date de publication: 2016/08/30
Omar Boukhobza
par 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
par 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
par 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(). 

 

More Questions Like This

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