问题
Can I and how could I call the main method from another method in Java? Please explain using the code below:
public class arraysAndMethods {
public void printArray(double[] arr) {
int x = public arraysAndMethods.main(int[] studGrades);
// pass array in main mehthod to other methods
//int a=main(args[]);
for (int i = 0; i < studGrades.lenght; i++)
System.out.print(studGrades[i] + " ");
}// end of printArray method
public static void main(String args[]){
java.util.Scanner input = new java.util.Scanner(System.in); // input scanner
System.out.println("What is the size of the class?");
int n = input.nextInt();
double studGrades[] = new double[n];
for (int i = 0; i < studGrades.length;i++) {
System.out.println("What is the grade of student #" + (i+1));
studGrades[i] = input.nextDouble();
} // end of for loop
}// end of main method
}//end of class
AFTER EDIT:
I've figured it out except for the passing in arguments. Do I use the return value from main? And since the return value from main is an array, should the parameters for the call of main have brackets? Should there be a value between those brackets?
public class arraysAndMethods {
public void printArray(double[] arr) {
int x = arraysAndMethods.main(double[i] arr);//error with paremeters
for (int i = 0; i < studGrades.lenght; i++)
System.out.print(studGrades[i] + " ");
}// end of printArray method
public static double[] main(String args[]){// double array
java.util.Scanner input = new java.util.Scanner(System.in); // input scanner
System.out.println("What is the size of the class?");
int n = input.nextInt();
double[] arr = new double[n];// declare and initialize array to have n many elements
for (int i = 0; i < arr.length;i++) {// input grades for each students
System.out.println("What is the grade of student #" + (i+1));
arr[i] = input.nextDouble();
} // end of for loop
return arr;
}// end of main method
}// end of class
回答1:
It is generally considered bad programming practice to call the main method from another method. This can lead to a whole list of problems such as infinite loops, infinite recursion, etc. Your main method should include high level functionality that your program needs to operate, you generally want it to be as simple as you can keep it. As a good kind of "check" ask yourself can I make this functionality into its own separate method? If so then why not do that? Then if you ever need to make that call again, you have a separate method to do so.
回答2:
Yes, you can call the main() method from another method. I've done this during unit tests to see how it works.
Since main is a static method, you'll want to call
arraysAndMethods.main(/* parameters here */);
with the simulated "command line arguments" passed in as a parameter.
FYI, it's standard Java practice to begin the name of a class with a capital letter.
回答3:
It can be called from another method. Within printArray()
(for instance) you can do this:
String[] someArgs= new String[] {"monkey"};
arraysAndMethods.main(someArgs);
As it is a static method the arrayAndMethods
class doesn't have to be instantiated.
来源:https://stackoverflow.com/questions/26429681/java-main-method-called