-1

Possible Duplicate:
Is the Main method must needed in a Java program?

Do java classes need to have a main method? If so, how can we use java classes without main method?

Community
  • 1
  • 1
kumar
  • 29
  • 1
  • 1

3 Answers3

2

A java class does not have to, but it can. You need at least one main method in your program to run, e.g. in Eclipse you can search for main functions and then build a run/debug configuration for it. You can have more than one main in your project, but of course you must choose one that is supposed to be run.

sth
  • 222,467
  • 53
  • 283
  • 367
InsertNickHere
  • 3,616
  • 3
  • 26
  • 23
1

No its not necessary java class should have main method. And purpose of main method is only to launch thread for application.

You can define class without main metod.

class address{

private String addr1;
private String addr2;
private String city;
private String state;
private String country;

//getter setter methods for above. no main method
}
luiscubal
  • 24,773
  • 9
  • 57
  • 83
YoK
  • 14,329
  • 4
  • 49
  • 67
0

The "main" method is the way that "java.exe" uses to start running your java program. In servlets your public classes conform to the servlet API, hence no main necessary.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347