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 ghj ghjh
Date Posted: 2016/08/30
Omar Boukhobza
by 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
by 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
by 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