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

Java 8Object Oriented ProgrammingProgramming

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”.

Example

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");
   }
}

Output

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
raja
Published on 08-Jan-2018 12:47:54
Advertisements