Multiple main() methods in java

后端 未结 10 584
忘了有多久
忘了有多久 2020-12-28 08:53

I was wondering what the effect of creating extra main methods would do to your code.

For example,

public class TestClass {
    public static void ma         


        
相关标签:
10条回答
  • 2020-12-28 09:08

    This is perfectly fine. Having multiple main methods doesn't cause any problems. When you first start a Java program, execution begins in some function called main in a class specified by the user or by the .jar file. Once the program has started running, all the other functions called main are essentially ignored or treated like other functions.

    0 讨论(0)
  • 2020-12-28 09:10

    It won't have an additional main-method, as main is static. So it's once per class.

    If you have multiple main-methods in your project, you will specify which one to launch when starting your application.

    0 讨论(0)
  • 2020-12-28 09:15

    It is all about the execution engine of JVM. Remember, you write >java com.abc.MainClass on cmd prompt.

    It explains everything. If main method is not found here it throws a run time Error:Main Method Not Found in class MainClass. Now if main method is found here, it acts as the first point when Program Counters have to map and start executing the instructions. Referred classes are loaded then, referred methods may be called using the instances created inside. So, main is class specific though one class can have only one main method. Please note, main method's signature never changes. You can have two overloaded main methods in same class, like

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

    public static void main() {} //overloaded in same class.

    During Static binding, the original main is resolved and identified by execution engine.

    0 讨论(0)
  • 2020-12-28 09:15

    Another interesting point to consider is a case where you have two different classes in of java file.

    For example, you have Java file with two classes:

    public class FirstClassMultiply {
    
        public static void main (String args[]){
            System.out.println("Using FirstClassMultiply");
            FirstClassMultiply mult = new FirstClassMultiply();
            System.out.println("Multiple is :" + mult.multiply(2, 4));
        }
        public static void main (int i){
            System.out.println("Using FirstClassMultiply with integer argument");
            FirstClassMultiply mult = new FirstClassMultiply();
            System.out.println("Multiply is :" + mult.multiply(2, 5));
        }
    
        int multiply(int a, int b) {
            return (a * b);
        }
    }
    
    class SecondClass {
    
        public static void main(String args[]) {
            System.out.println("Using SecondClass");
    
            FirstClassMultiply mult = new FirstClassMultiply();
            System.out.println("Multiply is :" + mult.multiply(2, 3));
    
            FirstClassMultiply.main(null);
            FirstClassMultiply.main(1);
        }
    }
    

    Compiling it with javac FirstClassMultiply.java will generate two .class files, first one is FirstClassMultiply.class and second one is SecondClass.class

    And in order to run it you will need to do it for the generated .class files: java FirstClassMultiply and java SecondClass, not the original filename file.

    Please note a couple of additional points:

    1. You will be able to run SecondClass.class although it's class wasn't public in the original file!
    2. FirstClassMultiply overloading of the main method of is totally fine, but, the only entry point to your prog will be the main method with String args[] argument.
    0 讨论(0)
  • 2020-12-28 09:17

    when you run your Java class it will always look for the signature public static void main(String args[]) in the class. So suppose if you invoking by command line argument, it will look for the method Signature in the class and will not invoke other until if u explicitly inoke it by its class name.

     class MainMethod1{
     public static void main(String[] ags){
       System.out.println("Hi main 1");
       testig2 y = new testig2();
    //in this case MainMethod1 is invoked/.......
     // String[] a = new String[10];
     // testig2.main(a);
      }
    
     }   
     class MainMethod2{
       public static void main(String[] ags){
       System.out.println("Hi main 2");
      }
     }
    

    But when you try the same from eclipse it will ask for which class to compile. Means MainMethod1 or Mainmethod2. So if te class has the exact signature they can be used as individual entry point to start the application. Coming to your question, If you remove the signature as u did above by changing the argument if main method. It will act as a normal method.

    0 讨论(0)
  • 2020-12-28 09:19

    You can have only one main method in one class, But you can call one main method to the another explicitly

    class Expmain
    {
        public static void main(String[] ar)
        {
            System.out.println("main 1");
        }
    }
    
    class Expmain1
    {
        public static void main(String[] ar)
        { 
            System.out.println("main 2");
            Expmain.main(ar);
        }
    }
    
    0 讨论(0)
提交回复
热议问题