Call "Basics" of methods in Java
First, call the methods in this class
Method one, declared as static by the called method, can be called directly in other methods. The sample code is as follows:
PublicClassHelloword {/** * @param args*/PublicStaticvoidMain (string[] args) {//TODO auto-generated Method Stub String str="helloword!";int a=0;int b=a+1;int result=0;Forint i=0;i<20;i++//add method call Result= Add (A, b); System. out.println (Str+ "" +< Span style= "color: #000000;" > result); A+=i;}} /** * called method, where static method is used to declare * @param x * @param y * @ return */private static int Add (int x, y) {return x+y;}}
Method Two, the called method, is not static modification, is not a statically method. Calls need to be made through instantiation of the class. Examples are as follows:
PublicClassHelloword {/** * @param args*/PublicStaticvoidMain (string[] args) {//TODO auto-generated Method Stub String str="helloword!";int a=0;int b=a+1;int result=0;Forint i=0;i<20;i++) {//Add method call//Instantiation of the class Helloword helloword=new Helloword (); // Add method calls by instantiating a class result= helloword. Add (A, b); System. out.println (Str+ "" +< Span style= "color: #000000;" > result); A+=i;}} /** * called method, not statically decorated, not static method. Calls need to be invoked through instantiation of the class * @param x * @param y * @return */private Span style= "color: #0000ff;" >int Add (int x,int Y) { Span style= "color: #0000ff;" >return x+y;}}
Second, call the method of the external class, through the instantiation of the class. The sample code is as follows:
External classes:
1PublicClassTest {2/**3* Called method add4* @param x5* @param y6* @return7*/8Publicint Add (int x,IntY9{1011Return x+Y12}1314/**15* Called method Sub16 * @param x 17 * @param y< Span style= "color: #008080;" >18 * @return 19 */20 public static int Sub (int x,int Y) 21 {22 23 return x-y; }25}
Call:
PublicClassHelloword {/***@paramArgs*/PublicStaticvoidMain (string[] args) {//TODO auto-generated Method Stub String str= "helloword!";int a=5;int b=a+1int result=0for (int i=0;i<20;i++ //add method call //new Test (); // Add method calls by instantiating a class result= test. Add (A, b); System.out.println (str+ "" + result); Result=test. Sub (b, 1); System.out.println (str+ "" + result); A+=i;}} }
Method calls in Java