two main methods in JAVA

here we look at control flow in case of two main methods.

1. by main we mean and only mean psvm i.e. public static void main(String[] args). 
2. changing anything in above signature will make the compiler not treat it as main

well of course the two mains cannot be in the same class. but consider these two classes. can you compute the output? results below....

public class Test1 {

    static{
        System.out.println("static of Test1");
    }

    public Test1() {
        System.out.println("Constructor of Test1");
    }

    public static void main(String[] args) {
        System.out.println("main of Test1");
    }

}

public class Test2 extends Test1 {

    static{
        System.out.println("static of Test2");
    }

    public Test2() {
        System.out.println("constructor of Test2");
    }

    public static void main(String[] args) {
        String[] array = {"hi", "hello"};
        Test1.main(array);
        System.out.println("main of Test2");

        Test1 t1 = new Test1();
    }
}



















































Output:
static of Test1
static of Test2
main of Test1
main of Test2
constructor of Test1

Comments

Popular Posts