Instance Main and Non-Instance Main method in one class example

In this post under Java language, I will show with example how Java runs a program which has two main methods.

Here two main methods means one traditional public static void main method as shown below

public static void main(String[] args) {
}

and another being the new main method as shown below

void main() {
}

Below is the complete class having both of them

Example

package core.language;
public class InstanceMainMethodExample2 {
public static void main(String[] args) {
System.out.println("Hello World From Non-Instance Main Method");
}
void main() {
System.out.println("Hello World From Instance Main Method");
}
}

As shown in the above code, it has two main methods.

The traditional main method prints “Hello World From Non-Instance Main Method” to the console.

The new main method prints “Hello World From Instance Main Method” to the console.

When we run the program, the JVM gives importance to traditional main method and calls it instead of new main method.

Below is the output

Output

Hello World From Non-Instance Main Method

In this way JVM behaves when they are two main methods (both traditional and new one) in a class.

Leave a comment