Question on methods

Hi I've been following along with tutorials on Oracle's page and I've been learning about methods. I had a question. I thought methods were a code that you wrote and then you can name it and it will be repeated, but I try to make a method called hey that prints out hey, and it only does it once, even though I named it four times. Why does this happen? If I take out the return from where it is, or put it elsewhere, it says:"Return statement missing" or "Return value not specified", respectively. Why is this happening? Also, a question about methods itself: since the things that go in parentheses are parameters, why do you need to put them? Why not just leave the parentheses blank? Aren't you limiting your options when you put in parameters? I may be wrong, as I dont quite understand parameters. (Sorry for the rogue question, but I didn't want to make another thread just for it.) Anyway, here's the code.

Re: Question on methods

It was a little hard to see where you defined your method. You defined it as

Java Code:

char hey(){
System.out.println("Hey");
return hey();
}

Do you know what the char is in char hey()? You do not always need parameters because you do not always need to pass anything to the method. A method is a block of code that may be repeated, but not always. Sometimes it makes it easier to break up a program into logical parts.

Re: Question on methods

You can put all your logic into the main method. However, it will get messy and impossible to read. I will go ahead and tell you a little bit about methods. When you declare (Create) a method, you have to include certain parts in naming it.

Java Code:

public static void main(String[] args){
//Code goes here
}

This is an example of a method, in this case it's the main method. You see how it is named?

public - Who can access it, don't worry about this too much right now
static - Association with the class or an object. Don't worry about this either
void - The return type. More on this in a bit
main - The name
() - Parameters go in here
String[] args - The actual parameters, an array of Strings.

The void is important, and seems to be what you are asking about right now. When you have void written there, it means that you don't have to return anything. There are different return types for example

Re: Question on methods

Thank you so, so much for the reply. It truly helped. But the thing is, now I'm confused on what return does. I thought it just ended the code...? what does static do? also, now that you have that method, how do you run it again, later on? Last thing: Does the main method being void mean that you cannot return values in it? Can you please explain a little more what return does? I can't find a proper tutorial to explain it :(. I express the deepest gratitude for your help, you are awesome :) Thanks, Michael

Because you declared them in two different classes, they cannot see each other.

Originally Posted by MW130

Thank you so, so much for the reply. It truly helped. But the thing is, now I'm confused on what return does. I thought it just ended the code...? what does static do? also, now that you have that method, how do you run it again, later on? Last thing: Does the main method being void mean that you cannot return values in it? Can you please explain a little more what return does? I can't find a proper tutorial to explain it :(. I express the deepest gratitude for your help, you are awesome :) Thanks, Michael

Return does almost exactly as it says. It returns a value, and yes it does end the portion of code.

The above wouldn't even compile because the print statement is never reached. When you return the number 5, the method returnFive ends. I would not worry about what Static does until you have a firm grasp of the basics and when you start to learn about objects. I don't want to raise more question in trying to solve these just yet. You can run the method repeatedly by calling it, again and again.

Yes, because the main value is return, it does not return anything. Return returns the value to the method that called it. In your case, it's the main method. There is nothing for the main method to return to, so therefore it is void.