Java programming 54-method call

Source: Internet
Author: User

Java programming those things 54-method call Zhengzhou game institute Chen yuefeng from: http://blog.csdn.net/mailbomb 7.4 method callA Method Declaration creates a new function. A declared method can call the function to execute the method as needed. A method can be executed only when it is called. There is a basic main method in the Java program. Its method declaration is as follows: public static void main (string [] ARGs). This method is included in the initial code framework, this is a special method. Java syntax stipulates that all j2se code is executed from this method. If a Code does not contain the main method, the Code cannot be directly run. Therefore, the main method is also called the entry method of the j2se program. when running the program, the main method in the corresponding code is automatically called To Start program execution. For the reason described above, if a method needs to be executed, it needs to be called directly or indirectly in the main method. When a method is called, the execution process of the program enters the internal method. After the return statement or the code inside the method is executed, return to the location where the method is called and continue to execute. The syntax of a method call is divided into the following two types: l the method call in a class refers to the call and the called method are both inside the class. L method calls between different classes indicate that the called and called methods are in different classes. Because the concept of the class has not yet been involved, it refers to the method calling syntax inside the class. The method calling between different classes will be introduced in subsequent chapters. 7.4.1 syntax for calling internal methods of a classIn the previous code framework, the following code declares the structure of the class: public class file name {the code in the braces after the declaration is called the internal of a class. When a method is called, the syntax format of the call is related to the static modifier. Therefore, there are two types of static modifiers for a method to be declared: l static modifier called static method l non-static modifier called non-static method there are four cases of method calls within a class: l call a non-static method in a non-static method l call a static method in a static method l call a non-static method in a static method the first three cases is called directly, the syntax format of direct call is: Method Name (parameter 1 value ,......); Here, the method name is the name of the called method, followed by a pair of parentheses, and the value of the parameter passed in when the method is called is written in sequence in parentheses, syntax requires that the number of input parameters and the type of each parameter must be consistent with the method declaration. The expression called here represents the return value of the method. values can be assigned as needed. The sample code is as follows: public class callmethod {public static void main (string [] ARGs) {int A = 10; int B = 2; int c = 3; int d = 32; max (a, B); // compare only. Int n = max (5, a) is lost after comparison; // compare, assign the return value to the variable n int M = max (c, d); // compare, assign the value of the return value to the variable M // compare the maximum values of the four numbers A, B, C, and D. Int maxnumber = max (a, B), max (C, d);} public static int max (int A, int B) {If (A> B) {return a ;}else {return B ;}}} in this example, the static Max method is called within the static main method, because the max method has two NT parameter, two int values must be input during the call, which can be an int variable or an int value. The basic format of the call is: max (parameter value 1, parameter value 2). The called expression can be arranged independently in the Code. When the return value type of the method is not void, you can receive or not receive the return value of a method. The return value of a method is a value of a definite type. Therefore, during the above comparison, nesting can be performed between method calls. Int maxnumber = max (a, B), max (c, d); where Max (A, B) is the maximum value of A and B, max (C, d) is to obtain the maximum values of C and D, and then compare the two obtained maximum values. The function of this Code is the same as that of the following code: int maxnumber = max (A, max (B, max (c, d); in general, this format is used for calls in the first three cases. In the last case, calling a non-static structure inside a static method is complex in syntax. The following is a simple example: public class callmethod2 {public static void main (string [] ARGs) {callmethod 2 cm = new callmethod2 (); int n = cm. max (1, 2);} public int max (int A, int B) {If (A> B) {return a ;}else {return B ;}}} the syntax format involved in this code will be explained in the subsequent code. Here we will only briefly describe it. Among them: callmethod 2 cm = new callmethod2 (); this line of code declares and creates a callmethod2 type object cm. The following code: int n = cm. max (1, 2); the object name is used when the max method is called. the method name calls the corresponding method. The parameter rules here are the same as described above. 7.4.2 execution process after method callDuring method calling, the execution process of the program is different from the previous structure. Simply put, when a method is called, the execution process of the program will jump to the called method and continue to run from the called position until the called method returns. The following is an example code to demonstrate the call process: public class callmethod3 {public static void main (string [] ARGs) {system. out. println (1); printtest (); system. out. println (2); max (10, 20); system. out. println (3);} public static int max (int A, int B) {system. out. println ("inside the max method! "); If (A> B) {return a;} else {return B;} public static void printtest () {system. Out. println (" Enter the printtest method! "); Int A = 10; system. Out. println (" The printtest method has been executed! ") ;}} The output during code execution is as follows: 1. Enter the printtest method! The printtest method has been executed! 2. Enter the max method! 3. From the output of program execution, we can clearly see the execution sequence of codes in the method call era.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.