1. Methods
method, Functions (function)//But essentially the same-implements specific functions.
A set of code that accomplishes independent functions and can be reused in a program;
Format of the method:
[modifier] Returns the type method name of the value ([formal parameter, ...])//Where modifiers, parameters can be omitted
{
Method body
return value;
}
Method must be called to take effect (think about going to the restaurant to order and go to the restaurant without ordering how to serve?) )
If the method uses the static modifier
At this point we use the name of the class where the method resides. Method name (parameter)
(If there is a method in Class A, you can only find Class A, and then use the method in Class A)
If the method does not use the static modifier
At this point we use the object of the class where the method is called (Object-oriented)
Public classMethoddemo {Static voiddoWork () {System.out.println ("Common 200 lines of code"); } Public Static voidMain (string[] args) {/** Assuming there is a, B, C, D four code snippets, each code has a repeating piece of code * assuming that the code needs to be maintained, then it is necessary to maintain four code fragments, so the public part can be written in a method * by means of method calls to save Time, and maintenance costs*/System.out.println ("Code Snippet A"); Methoddemo.dowork ();//Calling MethodsSYSTEM.OUT.PRINTLN ("code fragment B"); Methoddemo.dowork (); System.out.println ("Code snippet C"); Methoddemo.dowork (); System.out.println ("Code Snippet D"); }}
2. Method definition Format:
[modifier] Returns a value type method name {[parameter 1, Parameter 2, ...]}
{
Method body
[If the method needs to return a result to the caller, the return value is used at this time];
}
3. Concept Finishing
- Modifiers: public,static, static belongs to class, is called directly using the class name, and all uses static before understanding the object-oriented concept
- Return value type: The method actually completes a function, after the function is finished, whether to return a result to the caller. If you do not need to return the result to the caller, the keyword Void is declared, and no return is meant.
- Method Name: Follow the specification of the identifier, using a verb to denote, the first letter lowercase, if more than one word, using the hump white Oh is the law, the first letter of each word is capitalized, for example: GetEmployee
- Formal parameters: Methods The variables in parentheses are just placeholders, but the names of the parameters do not matter.
Java Method notes