A method is a section of code with a name, eg:
private void myTask(){
x=4;
y=5;
}
To call it up (invoke it) - we put:
myTask();
Example: A method which adds any two integers together, and leaves the result in sum. We declare it:
private void addUp(int a, int b) {
sum=a+b;
}
a and b are termed the 'formal parameters'
Here are some invocations/calls:
int d=7, e=3;Note:
addUp(5,6);
addUp(d,4);
addUp(d+e, 1);
class Fred.... {
private int sum, n = 0; // 'instance variables' -global to all methods of class
public aMethod(){
int n=3;
addUp(n,6);
}
private void addUp(int a, int b) {
sum=a+b;
}
}
--------------------
| |
----|CD-IN |
input | |
| AMP |----- to speaker....
sockets | |
| |
----|TAPE-IN |
| |
-------------------
private int addUp(int a, int b){
int temp;
temp=a+b;
return temp;
}
here are some calls:
int e,f;
e=addUp(3,4);
f=5 + addUp(e, 2);
label1.setText("answer is " + addUp(8,9));
addUp(5,6); //ERROR- nowhere for returned value to go!