Re: Dont know how to change my main method

You are very close. All you have to do is change your method header a bit:
Main methods in Java are written like this:

public static void main(String [ ] args)

Also, note that your main method should probably not return any variables.

Ignore my previous reply...you were right. So does that mean "public static void main(String [ ] args)" is similar to making an actual return type, if that makes any sense. What is the difference between it being void and declaring a return if they both are going to return something? Also, if I was to go about using the "return sum;" way, what would I have to change in my code?
Sorry for all of these questions, but I really am interested in this stuff and want to understand it thoroughly.
Thank you.

Re: Dont know how to change my main method

Posted 10 March 2013 - 10:03 PM

streek405, on 10 March 2013 - 09:14 PM, said:

Ignore my previous reply...you were right. So does that mean "public static void main(String [ ] args)" is similar to making an actual return type, if that makes any sense. What is the difference between it being void and declaring a return if they both are going to return something? Also, if I was to go about using the "return sum;" way, what would I have to change in my code?
Sorry for all of these questions, but I really am interested in this stuff and want to understand it thoroughly.
Thank you.

"What is the difference between it being void and declaring a return if they both are going to return something?"

The main method must be void. The completion of the main method does not necessarily indicate the end of the program. This means that your program could exit sometime during the main method. Once the program exits, there would be no sense in returning anything. So, basically the method is void because you don't know when exactly the program will end.

"Also, if I was to go about using the "return sum;" way, what would I have to change in my code?"

That really depends on why you need to return sum. Do you need it elsewhere in your code? If not, it would be much easier to just print it out in the main method.

If you really need to return it, though, here is a work-around:

1. Create another static method that returns an int. This method should accept an int parameter. The body of the method should return that parameter.

private static int returnSum(int sumToReturn ){
return sumToReturn;
}

2. Call this method inside of your main method using your sum variable: