简体   繁体   中英

Why a java program with multiple main methods is not giving any error?

File Name = multiple_main_methods.java

class multiple_main_methods_two {
    public static void main(String[] args){
        System.out.println("Class second");
    }
}
class multiple_main_methods_one {
    public static void main(String[] args){
        System.out.println("Class first");
    }
}

Output

Class first

IDE used - IntelliJ IDEA

IntelliJ is choosing a class to execute. Remember, you execute the main method in a class, not in a file.

Check it in your Run configurations enter image description here

It is not giving error because your main methods belong to different classes ie multiple_main_methods_two and multiple_main_methods_one which is completely fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM