Page 1 of 1

Passing Information Between Methods Arguments, Return Types and Overloading Rate Topic: -----

#1 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Posted 20 February 2010 - 04:02 PM

*
POPULAR

I see more than a few questions on the help forums that follow the general pattern of "I'm trying to pass an array between methods" or "Return type help?". After searching the tutorials I came to the conclusion that there is not a tutorial dealing solely with this topic - in fact, I didn't see it covered in any tutorials. So here is one:


Part I - Receiving Information:
I realize this may seem backwards to some, talking about receiving the information before it is sent, but really, this is logically the only way to talk about it. You wouldn't mail a package to a friends new address if they hadn't moved in yet, right? So before you worry about sending the information, worry about having somewhere set up to receive it.

Setting a method up to receive information is done in its signature, or declaration, inside the parentheses. This is known as the arguments list.

A basic method declaration looks like this:
public static void whatever() {
//code goes here
}


The above method cannot be passed any information. To get it to accept information, you need to declare a type (primitive OR class) and a name inside the parentheses. Ex:
public static void whatever(int x) {
//code goes here
}


Now the method, when it is called, will expect to be passed an integer value and will refer to it as x throughout the method body (the same as if you had declared the variable inside the method with that value). You can create multiple arguments, just separate them with a comma. Ex:
public static void whatever(int x, String y) {
//code goes here
}


The above method expects a value of type int, which it will call x, and a value of class String, which it will call y. Now let's briefly touch on how to send it that information.


Part II - Sending Information:
Information is sent when you call the method. A basic method call is just the method name, like this:
whatever();


But remember, our method is expecting first an int and then a String. If the method call does not include these, it will not work. So our method call will look like this:
whatever(5,"blah");


When the program comes across this code, it will send those values to the method whatever(int x, String y), which will than accept them into x and y respectively. Now in the above code, we only passed literals. However, you can pass the value of a variable, assuming the variable is of the correct type:
int a = 5;
String b = "blah";
whatever( a,b );


Remember that you must pass information as it is declared in the method signature - in this case, an int and THEN a String. If you try to pass an incorrect number of arguments, or arguments of the wrong type, or in the wrong order, it will not work. The following will not work for whatever(int x, String y):
//THESE DO NOT WORK
whatever(5);
whatever();
whatever(3,6,1,"blah");
whatever("blah",5);




Part III - Returning Information From a Method:

What if I want to pass information BACK to the calling method (the original method)? Let's take a look at our previous method declaration:
public static void whatever(int x, String y) {
//code goes here
}


In this method, the word void means that nothing is going to be returned. To make this method return something, we need to replace the word void with the type of information it will return:
public static int whatever(int x, String y) {
//code goes here
}


This method declaration says that the method whatever WILL return a value of type int. If the method declaration says a value will be returned, it is NOT optional. It must be returned and this is something you have to manually do. To return a value from a method, use the return keyword. Since our method is supposed to return an int, let's have it return an int:
public static int whatever(int x, String y) {
  return x;
}


As you might guess, the method now just echoes back whatever information it was sent for x. As with the arguments, a return type can be a literal (5,"blah", 'c', true) or a variable of the appropriate type, as we have done above. However, UNLIKE arguments, you cannot return multiple values.


Part IV - Handling Returned Information:
Now that we have a method that returns information, we need to do something with the returned info. You can assign it to a variable (which of course must be the same type as the declared return type):
int c = whatever(5,"blah");


You can use it in an expression:
int c = 4 + whatever(5,"blah");


You can even do nothing with it:
whatever(5,"blah");


In the last code, the returned value of whatever() just disappears. This is not necessarily rare, but if you've gone to the trouble of declaring a return type and actually returning a value, chances are you'll want to do something with it.


Part V - Returning Multiple Values:
Remember when I said you couldn't return multiple values? Technically, I lied. With return types and arguments, you can use ANY primitive variable, data structure or class. And while you can only return one of these, there is no problem with returning one of these that contains multiple values. An array for instance:
public static void main(String[] args) {
  int[] = whatever(5,3,2);
}

public static int[] whatever(int x,int y,int z) {
  int[] a = {x,y,z};
  return a;
}


You could even create your own class with as many variables as you'd like, and then return an instance of that class.


Part VI - Overloading Methods:
But what if sometimes I only need to pass one value, and another time I need to pass two? You can pass different numbers of values to a method by overloading it (this seems to be most commonly done with constructors). Overloading it means that you define it more than once. If you need whatever() to be able to sometimes accept one int, and sometimes two, you could declare it twice, with one declaration accepting one int, and the other two. Ex:
public static int whatever(int x) {
  return x;
}

public static int whatever(int x, int y) {
  return x;
}


With the above code, both of the following calls are valid:
int a = whatever(5);
int b = whatever(5,3);


However, all the old rules still apply. If there is no method declaration that matches the arguments you're trying to send, it will not work. You still need to have a method that is expecting the number and type of arguments you're trying to send.

Part VII - Summary:
To make a method accept information that is passed to it, a variable type and name must be in the parentheses in the method declaration:
public static void whatever(int x) {}



Information is passed to a method when it is called, and is inside the parentheses:
whatever(5);



Return types must be declared in the method declaration:
public static int whatever(int x) {}



Information must be manually returned using the return keyword:
public static int whatever(int x) {
  return x;
}



You can pass and return literals (4,"asdf",'z') and variable values.
You can pass and return ANY primitive variable, or ANY object. This includes booleans, ints, floats, Strings, arrays.

Is This A Good Question/Topic? 7
  • +

Replies To: Passing Information Between Methods

#2 Java Hardstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 04-September 12

Posted 12 September 2012 - 01:00 PM

Thank you! This is so essential when starting to learn Java.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1