Search code examples
javaclassconstructordestructorjava-compiler-api

What is the order of creating and destroying main class and its superclass


Given that execution starts from main method how can super-class instance be created before main class instance and if given that super class must be initialized first before main class, will the Super-class of the main class be first destroyed or destroying main class first would harm the program if necessary?

public class Main extends JFrame {

       public static void main(String args[]){

                super("Title of my prog");       // Superclass method
                //code
                //code
            }

      }

Solution

  • The reason that main must be static is precisely so that the JVM doesn't have to construct an instance for the main class before it starts the program's execution, and it's quite common for a main class never to be instantiated.