It is very important to understand how the parameters are passed from the calling method to the called method and how the return value is sent back to the calling method. As discussed in
Basic Java Methods, the
main
method is the
calling method and
convertToCelsius
is the
called method.
Lets look at another example to understand the parameters passing better.
public static void main(String s[])
{
int a = 5;
int b = 6;
int c = sum(a, b);
}
public static int sum(int a, int b)
{
int c = a + b;
return c;
}
Here we have created a
main
method and a
sum
method.
main
method is calling method and
sum
is the called method. Please note the following points about these methods.
- The parameter names in the calling method and the called method need not be same as shown above. In the method
sum
, the parameters could be called x
and y
instead of a
and b
. Only the data types should be same.
- The name of the return variable also need not be same as the calling method. The return value
c
could have been z
.
// Valid
public static int sum(int x, int y)
{
int z = x + y;
return z;
}
- It is not necessary that we create a variable for returning, we can directly return with out creating the variable.
// Valid
public static int sum(int x, int y)
{
return x + y;
}
- Only variables passed as parameters are accessible in the called method. For eg. the variable
r
created in the main
method is not accessible in the sum
method.
public static void main(String s[])
{
int a = 5;
int b = 6;
int r = 21;
int c = sum(a, b);
}
public static int sum(int a1, int b1)
{
int c1 = a1 + b1;
System.out.println(" r = " + r); // Compilation Error.
return c1;
}
- Similarly, any new variables created in the called method are not accessible in the calling method. For eg. the variable
m
created in the sum
method is not accessible in the main
method.
public static void main(String s[])
{
int a = 5;
int b = 6;
int c = sum(a, b);
System.out.println(" m = " + m); // Compilation Error.
}
public static int sum(int a, int b)
{
int c = a + b;
int m = 78;
return c;
}
- Even if the variables have same name in both the calling method and the called method, since their scope is different, the changes done on one variable will not impact the other. Here the variable
k
in the main
method and the k
in the sum
method are different, changes made to one will not impact the other.
public static void main(String s[])
{
int a = 5;
int b = 6;
int k = 78;
int c = sum(a, b);
// k's value will be 78
}
public static int sum(int a, int b)
{
int c = a + b;
int k = 88; // Declare k again and initialize to 88, this will not impact the k in the main method.
return c;
}
- Even if the parameters have the same name, if the data types are different then it throws a compilation error. As shown below, the data types in the
main
method are int
where as they are boolean
in the called method sum
, hence it throws a compilation error.
public static void main(String s[])
{
int a = 5;
int b = 6;
int c = sum(a, b); // Compilation error
}
public static int sum(boolean a, boolean b)
{
return a && b ? 25 : 45;
}