Method calls
Java supports two ways to invoke a method, depending on whether the method returns a value.
When a program calls a method, the control of the program is given to the method being invoked. The control is returned to the program when the return statement of the invoked method executes or arrives at the method body closing parenthesis.
When a method returns a value, the method call is usually treated as a value. For example:
int larger = max (30, 40);
If the method return value is void, the method call must be a statement. For example, method println returns void. The following call is a statement:
System.out.println ("Welcome to java!");
Example
The following example shows how to define a method and how to invoke it:
public class Testmax {
/** Main method *
/public static void main (string[] args) {
int i = 5;
int j = 2;
int k = Max (i, j);
System.out.println ("The maximum between" + i + "and
" + j + "is" + K);
}
/** returns a larger value of two integer variables *
/public static int max (int num1, int num2) {
int result;
if (Num1 > num2) result
= NUM1;
else result
= num2;
return result;
}
The results of the above instance compilation run are as follows:
The maximum between 5 and 2 is 5
This program contains the main method and the Max method. The main method is called by the JVM, except that the main method is no different from the other methods.
The Main method's head is invariant, as shown in the example, with modifiers public and static, and returns the void type value, the method name is main, with a string[] type parameter. String[] Indicates that the argument is an array of strings.
Invoke instance:
One, call the methods in this class
Method One, the invoked method is declared static, and can be called directly in other methods. The sample code is as follows:
public class Helloword {
/**
* @param args */public
static void Main (string[] args) {
//TODO Auto-ge nerated method Stub
String str= "helloword!";
int a=0;
int b=a+1;
int result=0;
for (int i=0;i<20;i++)
{
//add method calls
result= Add (a,b);
System.out.println (str+ "" + result);
A+=i
}
}
/**
* is called method, where static is declared as the method
* @param x
* @param y
* @return
/private static int Add ( int X,int y)
{return
x+y
}
}
Method Two, the called method, is not modified by static, not static method. Calls need to be invoked through the instantiation of the class. Examples are as follows:
public class Helloword {
/**
* @param args */public
static void Main (string[] args) {
//TODO Auto-ge nerated method Stub
String str= "helloword!";
int a=0;
int b=a+1;
int result=0;
for (int i=0;i<20;i++)
{
//add
instantiation
helloword helloword=new Helloword () for the method call//Class);
The Add method calls the Result=helloword through the instantiated class
. Add (A, b);
System.out.println (str+ "" + result);
A+=i
}
}
/**
* Called method, is not modified by static, not static method. Calls need to be called through the instantiation of the class
* @param x
* @param y
* @return * *
private int Add (int x,int y)
{
Return x+y
}
}
Second, the method of calling the outer class is invoked through the instantiation of the class. The sample code is as follows:
External classes:
The public class Test {
/**
* is called method Add
* @param x
* @param y
* @return/public
int Add (int x,int Y)
{return
x+y;
}
/**
* Called method Sub
* @param x
* @param y
* @return/public
static int Sub (int x,int y)
{ return
x-y;
}
}
Call:
public class Helloword {
/**
* @param args */public
static void Main (string[] args) {
//TODO Auto-ge nerated method Stub
String str= "helloword!";
int a=5;
int b=a+1;
int result=0;
for (int i=0;i<20;i++)
{
//add
instance
test test=new test () for the method call//Class);
The Add method calls the Result=test through the instantiated class
. Add (A, b);
System.out.println (str+ "" + result);
Result=test. Sub (b, 1);
System.out.println (str+ "" + result);
A+=i
}}
}