原文源自http://www.jb51.net/article/73827.htm
方法调用
Java支持两种调用方法的方式,根据方法是否返回值来选择。
 当程序调用一个方法时,程序的控制权交给了被调用的方法。当被调用方法的返回语句执行或者到达方法体闭括号时候交还控制权给程序。
 当方法返回一个值的时候,方法调用通常被当做一个值。例如:
如果方法返回值是void,方法调用一定是一条语句。例如,方法println返回void。下面的调用是个语句:
System.out.println("Welcome to Java!");
示例
 下面的例子演示了如何定义一个方法,以及如何调用它:
public class TestMax { /** 主方法 */ 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); } /** 返回两个整数变量较大的值 */ public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } } 以上实例编译运行结果如下: The maximum between 5 and 2 is 5
这个程序包含main方法和max方法。Main方法是被JVM调用的,除此之外,main方法和其它方法没什么区别。
main方法的头部是不变的,如例子所示,带修饰符public和static,返回void类型值,方法名字是main,此外带个一个String[]类型参数。String[]表明参数是字符串数组。
调用实例:
一、调用本类中的方法
方法一、被调用方法声明为static ,可以在其他方法中直接调用。示例代码如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | publicclassHelloWord {  /**   * @param args   */  publicstaticvoidmain(String[] args) {    // TODO Auto-generated method stub    String str="HelloWord!";    inta=0;    intb=a+1;    intresult=0;        for(inti=0;i<20;i++)    {      //Add方法调用      result= Add(a,b);                  System.out.println(str+" "+ result);            a+=i;          }  }  /**   * 被调用方法,这里使用了static声明为静态方法   * @param x    * @param y   * @return   */  privatestaticintAdd(intx,inty)  {        returnx+y;  }} | 
方法二、被调用方法,没被static修饰,不是静态方法。调用时需要通过类的实例化进行调用。示例如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | publicclassHelloWord {  /**   * @param args   */  publicstaticvoidmain(String[] args) {    // TODO Auto-generated method stub    String str="HelloWord!";    inta=0;    intb=a+1;    intresult=0;        for(inti=0;i<20;i++)    {      //Add方法调用      //类的实例化      HelloWord helloword=newHelloWord();            //通过实例化的类进行Add方法调用      result=helloword.Add(a, b);                        System.out.println(str+" "+ result);            a+=i;          }  }  /**   * 被调用方法,没被static修饰,不是静态方法。调用时需要通过类的实例化进行调用   * @param x    * @param y   * @return   */  privateintAdd(intx,inty)  {        returnx+y;  }} | 
二、调用外部的类的方法,通过类的实例化进行调用。示例代码如下:
外部的类:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | publicclassTest {  /**   * 被调用方法Add   * @param x    * @param y   * @return   */  publicintAdd(intx,inty)  {        returnx+y;  }    /**   * 被调用方法Sub   * @param x    * @param y   * @return   */  publicstaticintSub(intx,inty)  {        returnx-y;  }} | 
调用:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | publicclassHelloWord {  /**   * @param args   */  publicstaticvoidmain(String[] args) {    // TODO Auto-generated method stub    String str="HelloWord!";    inta=5;    intb=a+1;    intresult=0;        for(inti=0;i<20;i++)    {      //Add方法调用      //类的实例化      Test test=newTest();            //通过实例化的类进行Add方法调用      result=test.Add(a, b);                        System.out.println(str+" "+ result);            result=test.Sub(b, 1);                        System.out.println(str+" "+ result);            a+=i;          }  }  } | 
方法调用
Java支持两种调用方法的方式,根据方法是否返回值来选择。
 当程序调用一个方法时,程序的控制权交给了被调用的方法。当被调用方法的返回语句执行或者到达方法体闭括号时候交还控制权给程序。
 当方法返回一个值的时候,方法调用通常被当做一个值。例如:
| 1 | intlarger = max(30, 40); | 
如果方法返回值是void,方法调用一定是一条语句。例如,方法println返回void。下面的调用是个语句:
| 1 | System.out.println("Welcome to Java!"); | 
示例
 下面的例子演示了如何定义一个方法,以及如何调用它:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | publicclassTestMax {  /** 主方法 */  publicstaticvoidmain(String[] args) {   inti = 5;   intj = 2;   intk = max(i, j);   System.out.println("The maximum between "+ i +          " and "+ j + " is "+ k);  }  /** 返回两个整数变量较大的值 */  publicstaticintmax(intnum1, intnum2) {   intresult;   if(num1 > num2)     result = num1;   else     result = num2;   returnresult;   }} | 
以上实例编译运行结果如下:
| 1 | The maximum between 5 and 2 is 5 | 
这个程序包含main方法和max方法。Main方法是被JVM调用的,除此之外,main方法和其它方法没什么区别。
main方法的头部是不变的,如例子所示,带修饰符public和static,返回void类型值,方法名字是main,此外带个一个String[]类型参数。String[]表明参数是字符串数组。
调用实例:
一、调用本类中的方法
方法一、被调用方法声明为static ,可以在其他方法中直接调用。示例代码如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | publicclassHelloWord {  /**   * @param args   */  publicstaticvoidmain(String[] args) {    // TODO Auto-generated method stub    String str="HelloWord!";    inta=0;    intb=a+1;    intresult=0;        for(inti=0;i<20;i++)    {      //Add方法调用      result= Add(a,b);                  System.out.println(str+" "+ result);            a+=i;          }  }  /**   * 被调用方法,这里使用了static声明为静态方法   * @param x    * @param y   * @return   */  privatestaticintAdd(intx,inty)  {        returnx+y;  }} | 
方法二、被调用方法,没被static修饰,不是静态方法。调用时需要通过类的实例化进行调用。示例如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | publicclassHelloWord {  /**   * @param args   */  publicstaticvoidmain(String[] args) {    // TODO Auto-generated method stub    String str="HelloWord!";    inta=0;    intb=a+1;    intresult=0;        for(inti=0;i<20;i++)    {      //Add方法调用      //类的实例化      HelloWord helloword=newHelloWord();            //通过实例化的类进行Add方法调用      result=helloword.Add(a, b);                        System.out.println(str+" "+ result);            a+=i;          }  }  /**   * 被调用方法,没被static修饰,不是静态方法。调用时需要通过类的实例化进行调用   * @param x    * @param y   * @return   */  privateintAdd(intx,inty)  {        returnx+y;  }} | 
二、调用外部的类的方法,通过类的实例化进行调用。示例代码如下:
外部的类:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | publicclassTest {  /**   * 被调用方法Add   * @param x    * @param y   * @return   */  publicintAdd(intx,inty)  {        returnx+y;  }    /**   * 被调用方法Sub   * @param x    * @param y   * @return   */  publicstaticintSub(intx,inty)  {        returnx-y;  }} | 
调用:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | publicclassHelloWord {  /**   * @param args   */  publicstaticvoidmain(String[] args) {    // TODO Auto-generated method stub    String str="HelloWord!";    inta=5;    intb=a+1;    intresult=0;        for(inti=0;i<20;i++)    {      //Add方法调用      //类的实例化      Test test=newTest();            //通过实例化的类进行Add方法调用      result=test.Add(a, b);                        System.out.println(str+" "+ result);            result=test.Sub(b, 1);                        System.out.println(str+" "+ result);            a+=i;          }  }  } | 
原文:http://www.cnblogs.com/limingxian537423/p/6914823.html