Can a main method in Java return something?

Q 1. Can we write a java class with : public static int main(String[] args){

Yes, you can but you can’t run that Java class.

Example class:

class MainTest {

        public static int main(String[] args) {
                return 1;
        }
}

You will receive an error message when trying to run it:

Error: Main method must return a value of type void in class MainTest, please 
define the main method as:
   public static void main(String[] args)

Q 2: Next question I unable to answer. He asked write a program so
that your java main method could return something.

You can use System#exit(int) to quit your program with a specific exit code which can be interpreted by the operating system.

Leave a Comment