• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can we run Java programs without public static void main method?  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we run programs without  main method
by declaring public class and driving the class by using annotation .... to determine which method to execute

why is java dependent on the main method.
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there is no annotation which you can use to tell Java where to start executing the program.

Why: When you run a Java program, the Java runtime environment has to start running your program somewhere. Long ago, when Java was invented, it was decided that it would always start at the main method. So, to answer your question why: because that's how Java was designed to work when it was invented in 1995 or so.
 
narayan lakshmis
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you ....for your help

So main method drives java programs ....no main method means no java program.....
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is probably because C and C++ used a similar naming for the function used to start an application. It used to be possible to use a static initialiser:-. . . but even that trick stopped working in Java7. Try a Java6 installation
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!