Why the main method has to be in a java class?

Main method is the entry point of the execution in Java. When we execute a class JVM searches for the main method and execute the contents of it line by line.

If you observe the following example you can compile a this program but if you try to execute it you will get an error saying “Main method not found”.


abstract class SuperTest {
   public abstract void sample();
   public abstract void demo();
public class Example extends SuperTest{
   public void sample(){
      System.out.println("sample method of the Example class");
   public void demo(){
      System.out.println("demo method of the Example class");


C:\Sample>javac Example.java
C:\Sample>java Example
Error: Main method not found in class Example, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

Updated on: 30-Jul-2019


Kickstart Your Career

Get certified by completing the course

Get Started