What will happen, when two main method in a program of different classes is being defined? Who will be called first?

Answer:
If there are two classes in the program, each classes have their own main method. Then only main method of main class in the program will be called. But both class will be compiled.


Program:

// define a class four, in which change the place of static, public in main method
class Four
{
    static public void main(String args[])//change
    {
        //call print function
        System.out.println("hello java");
    }
}
// define another class
class Five
{
    //main method of program of class Five
    public static void main(String args[])
    {
        System.out.println("main method of class five");
    }
}


Sample output:

Post a Comment

Previous Post Next Post