The following code has a method that subtracts the two pieces of data passed to it. Watch the names of the variables inside the method and outside it.
public class minus
{
public static void main (String args [])
{
int x = 34;
int y = 88;
int z;
//function call
z = subtractor (y, x);
System.out.println ("The difference is: " + z);
}
public static int subtractor (int a, int b) //function declaration
{ //returns a subtract b
int c;
c = a - b;
return c;
}
}
The variables passed to a method are called parameters. We generally look at parameters from two viewpoints: inside the method and outside the method.
In the examples that we have looked at so far, the parameters inside the method have always had a different name than those outside the method. In the subtractor example, the parameters inside the method were referred to as a and b. In the main method, when we called the method, we referred to the parameters as y and x. The computer uses the order of the parameters to match up the different names. Therefore, y is the same as a and x is the same as b.
To help make this a little less confusing, different names are given to the parameters depending on where you are in the program. The parameters inside the method are formal parameters, when the method is being called (in main or wherever) they are actual parameters.
Formal and actual parameters do not need to be named differently. In the subtractor example, you could replace all of the y’s with a’s and all of the x’s with b’s and it would run just fine. The programmer has the option of naming them the same or different as they choose. The gives us more flexibility when we code.
Variables can be declared outside methods,
inside methods and inside main. Where they are declared effects how they can
be used.
|
|
If a variable is declared outside all methods, including the main method, it is a global variable and can be used everywhere in the program. If the value stored in the variable is changed anywhere in the program, it is changed for all of the other parts of the program. If a variable is declared anywhere else (including the main function) it is a local variable and can only be used in that method. |
What if you declared variable in the file, in the main method and in other methods and gave them all the same name. Would any changes that you made to the local variables affect the global variable with the same name? This code (which does nothing of interest) experiments with that question.
import java.io.*;
public class scope
{
int x = 1; //global variable
public static void main (String args [])
{
System.out.println ("1: " + x);
int x = 2;
System.out.println ("2: " + x);
zztop ();
yip ();
}
public static void zztop ()
{
System.out.println ("3: " + x);
}
public static void yip ()
{
System.out.println ("4: " + x);
int x = 3;
System.out.println ("5: " + x);
}
}
The rule shown by this example is that if a local and global variable have the same name, then the local variable overrides the global variable in that subroutine/method after it is declared.