Can multiple main method of class A can be called from another class B main method

I want to call the main method of the same class with different string arguments from another class main method.

public class A {

    public static void main(String[] args) {

        String[] testArgs = {"Hi", "Helloworld" };

        B.main(testArgs );

        String[] testArgs1 = {"Hi", "Java" };

        B.main(testArgs1 );

    }

}

public class B {

    public static void main(String[] args) {

        System.out.println(args[0] + " " + args[1]);
    }

}

2
Leave a Reply

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

Yes, you can call a main method exactly like you would call any other static method, from within the same class or from another class (as you already do). 1) If you put two classes, like A and B, in the same source file, exacly one of must be declared public. The name of this public class must be the prefix of the name of the file. (So, if A is the public class, the name of the file must be A.java, if stored in a ordinary file system.) 2) If you put A and B in separate source files,… Read more »

Jason
Guest

Yes you can ,but you just need to remove “public” from class B because in java a file have only one public class.